作者stellvia7 (stellvia)
看板C_Sharp
标题Re: [问题] 关於 c# dllimport dll 传入 char* 参数
时间Tue Jan 12 13:22:31 2010
[System.Runtime.InteropServices.DllImportAttribute("hyde_capture_dll.dll")]
private static extern int go_capture([MarshalAs(UnmanagedType.LPStr)]
string name);
这样用可是每当执行到会有exception 丢出..搞不懂烦请指点
※ 引述《stellvia7 (stellvia)》之铭言:
: dll 中
: int go_capture(char *name)
: {
: printf("the name = %s\d",name);
: }
: c# 中
: [System.Runtime.InteropServices.DllImportAttribute("test.dll")]
: private static extern int go_capture(char *name);
: ...
: ...
: ...
: void button_click(....)
: {
: string abc="temp_name";
: go_capture(abc);
: }
: 上述 go_capture的用法我知道是错的 但我try很久 有的说用unsafe
: 有个要用Inptr转型 但是一直失败 可以烦请指点吗..
: 谢谢
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.130.36.18
1F:→ remmurds:.............先去MSDN查一下DllImportAttribute怎麽用吧 01/12 13:25
2F:→ stellvia7:我DllImportAttribute 用法应该没错阿 除了传pointer有 01/12 14:16
3F:→ stellvia7:关的会有问题 其余 我call其他function 不用传参数的 01/12 14:17
4F:→ stellvia7:都work ok阿 01/12 14:17
5F:推 cplusplus:你要不要直接用string试试看 理论上应该会帮你做转换 01/13 00:46
6F:推 leicheong:我会用StringBuilder, 它会自行处理各种C string间的 01/15 23:46
7F:→ leicheong:转换外更会替你保护pointer指向那块buffer... 转入是用 01/15 23:47
8F:→ leicheong:不到啦, 但是在会转出的时候就很重要. 01/15 23:48