首页 新闻 会员 周边

以下下这段Python代码对应的c#实现方式是什么样子的,求指教

0
悬赏园豆:10 [已解决问题] 解决于 2018-01-04 14:52

raw_memory = bytearray(buf_size)
ctypes_raw_type = (ctypes.c_char * buf_size)
ctypes_raw_memory = ctypes_raw_type.from_buffer(raw_memory)
python
这段代码翻译成C#对应的代码是什么样子的

tuohaibei的主页 tuohaibei | 初学一级 | 园豆:46
提问于:2018-01-03 19:30
< >
分享
最佳答案
0

C# 中可能就是 Buffer.MemoryCopy()

收获园豆:10
dudu | 高人七级 |园豆:30994 | 2018-01-03 23:06

能给下具体应用吗?谢谢

tuohaibei | 园豆:46 (初学一级) | 2018-01-04 09:33

@tuohaibei: 见 Buffer.MemoryCopy

dudu | 园豆:30994 (高人七级) | 2018-01-04 09:42

@dudu: 你好,刚才看了代码了,是直接内存内实现拷贝的,我的代码可能上下文不足,我再补充一下代码:
buf_size = 0x1000
raw_memory = bytearray(buf_size)
ctypes_raw_type = (ctypes.c_char * buf_size)
ctypes_raw_memory = ctypes_raw_type.from_buffer(raw_memory)
encLen = Objdll.encode(byref(ctypes_raw_memory), buf_size,inputCode,len(inputCode))
print "len:",encLen
print raw_memory[0];

print "ret len:",len(szPara.value)

return raw_memory[:encLen]

其中Objdll.encode属于第三方方法,用了ctypes_raw_memory ,不知道用你你说的方法ctypes_raw_memory怎么来生成?

tuohaibei | 园豆:46 (初学一级) | 2018-01-04 11:21

@tuohaibei: 试试 Marshal.Copy ,示例代码:

byte[] buffer = new byte[0x1000];
IntPtr intPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, intPtr, buffer.Length);
Marshal.FreeHGlobal(intPtr);
dudu | 园豆:30994 (高人七级) | 2018-01-04 11:51

@dudu: 大牛不愧为大牛,非常感谢。纠结的问题解决了。10分感谢

tuohaibei | 园豆:46 (初学一级) | 2018-01-04 13:01

@dudu: 下午调试还是有问题?传同样的值生成的字节流结果不一样,不清楚是哪里的问题,以下为结果:左边为python生成,右边为c#生成(inputCode传的值是4545454545444343)

tuohaibei | 园豆:46 (初学一级) | 2018-01-04 19:23

@tuohaibei: C#代码具体是怎么写的?

dudu | 园豆:30994 (高人七级) | 2018-01-04 20:23

@dudu: byte[] buffer = new byte[0x1000];
IntPtr intPtr Marshal.AllocHGlobal(buffer.Length);
int encLen = encode(intPtr, 0x1000, "4545454545444343", "4545454545444343".Length);
Marshal.Copy(intPtr, buffer, 0, 0x1000)
Console.ReadKey();
}

[DllImport("NEWDLL.dll", EntryPoint = "encode", CharSet = CharSet.Ansi)]

public static extern int encode(IntPtr memory, int buf_size, string inputCode, int strLen);

我读的就是buffer里边的值.和上边python raw_memory[:encLen]里的不一样

tuohaibei | 园豆:46 (初学一级) | 2018-01-04 23:06

@tuohaibei: 将 Marshal.AllocHGlobal(buffer.Length) 改为 Marshal.AllocHGlobal(Marshal.SizeOf(typeof(byte)) * buffer.Length); 试试

dudu | 园豆:30994 (高人七级) | 2018-01-05 09:20

@dudu: 效果还是一样,我看python代码
“ctypes_raw_type = (ctypes.c_char * buf_size)
ctypes_raw_memory = ctypes_raw_type.from_buffer(raw_memory)“
这个地方有一个ctypes.c_char 字符类型的变量,而我们c#代码却没有,是不是与这个有关系?

tuohaibei | 园豆:46 (初学一级) | 2018-01-05 10:34

@tuohaibei: NEWDLL.dll 中 encode() 方法原始方法参数是如何定义的?

dudu | 园豆:30994 (高人七级) | 2018-01-05 10:54

@dudu: 这个地方我也不太清楚,这是给我们提供服务的人提供的,我看python代码:
Objdll.encode(byref(ctypes_raw_memory), buf_size,inputCode,len(inputCode)),
是这样定义的byref(ctypes_raw_memory),好像是地址引用,所以我再c#里边用的时候是这样定义的。
[DllImport("NEWDLL.dll", EntryPoint = "encode", CharSet = CharSet.Ansi)]
public static extern int encode(IntPtr memory, int buf_size, string inputCode, int strLen);
第一个参数用的指针类型,刚开始用的memorystream,也可以。我们这边有几个老的调用

[DllImport("NEWDLL.dll", EntryPoint = "decheckcode", CharSet = CharSet.Ansi)]
public static extern int decheckcode(byte[] outputstring, int outBufferSize, byte[] input);

    [DllImport("NEWDLL.dll", EntryPoint = "checkcode", CharSet = CharSet.Ansi)]
    public static extern int checkcode(byte[] outputstring, int outBufferSize, byte[] input, byte[] o, byte[] p, int q);

    [DllImport("NEWDLL.dll", EntryPoint = "gettoken", CharSet = CharSet.Ansi)]
    public static extern int gettoken(byte[] outputstring, int outBufferSize, byte[] input);

    [DllImport("NEWDLL.dll", EntryPoint = "getHashCode", CharSet = CharSet.Ansi)]
    public static extern int getHashCode(byte[] outputstring, int outBufferSize, byte[] input);

但这些是以前的,流程有变,原来的那些方法也用不了了.
关于这个接口我问下给我们提供服务的人.

tuohaibei | 园豆:46 (初学一级) | 2018-01-05 11:04

@tuohaibei: 首先要确认C#中对应的public static extern定义是否正确

dudu | 园豆:30994 (高人七级) | 2018-01-05 11:09

@dudu: 我们之前有对应的代码,帮忙参考一下:

对应的c#代码是

现在python代码是

tuohaibei | 园豆:46 (初学一级) | 2018-01-05 15:57

@dudu: 不清楚转成c#代码应该怎么样?

tuohaibei | 园豆:46 (初学一级) | 2018-01-05 16:25

@tuohaibei: 抱歉,我这边没法进行测试,总之就是这个思路,你可以多尝试一下

dudu | 园豆:30994 (高人七级) | 2018-01-05 16:47

@dudu: 方便的话,能帮我调试一下吗?我的QQ是418566447.万分感谢

tuohaibei | 园豆:46 (初学一级) | 2018-01-05 17:01

@tuohaibei: 需要把 NEWDLL.dll 发给我,就在博问中交流

dudu | 园豆:30994 (高人七级) | 2018-01-05 17:06

@dudu: 可以,只不过怎么发给你?

tuohaibei | 园豆:46 (初学一级) | 2018-01-05 17:34

@tuohaibei: 可以了,纠结了这么久,原来我一直用了一个没有注册过的newdll

tuohaibei | 园豆:46 (初学一级) | 2018-01-05 20:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册