Re: [問題] 關於 c# dllimport dll 傳入 char* 參數

看板C_Sharp作者 (stellvia)時間16年前 (2010/01/12 13:22), 編輯推噓2(206)
留言8則, 4人參與, 最新討論串2/2 (看更多)
[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

01/12 13:25, , 1F
.............先去MSDN查一下DllImportAttribute怎麼用吧
01/12 13:25, 1F

01/12 14:16, , 2F
我DllImportAttribute 用法應該沒錯阿 除了傳pointer有
01/12 14:16, 2F

01/12 14:17, , 3F
關的會有問題 其餘 我call其他function 不用傳參數的
01/12 14:17, 3F

01/12 14:17, , 4F
都work ok阿
01/12 14:17, 4F

01/13 00:46, , 5F
你要不要直接用string試試看 理論上應該會幫你做轉換
01/13 00:46, 5F

01/15 23:46, , 6F
我會用StringBuilder, 它會自行處理各種C string間的
01/15 23:46, 6F

01/15 23:47, , 7F
轉換外更會替你保護pointer指向那塊buffer... 轉入是用
01/15 23:47, 7F

01/15 23:48, , 8F
不到啦, 但是在會轉出的時候就很重要.
01/15 23:48, 8F
文章代碼(AID): #1BJ0SQui (C_Sharp)
文章代碼(AID): #1BJ0SQui (C_Sharp)