作者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