![](/img/trans.png)
[英]The third-party DLL works when called from Excel and crashes in the case of call from VisualBasic/C#
[英]Call VisualBasic DLL in C# works but how to replace a Pchar?
我有一個用VisualBasic編寫的舊DLL,需要調用C#中的函數。 15個功能中的14個已經在運行(已通過控制台項目測試)。 剩余的函數有一個參數PChar,我不知道如何在C#中調用它。
工作功能如下所示:
[DllImport("C:\\DLL\\visualbasic.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
public static extern int command(out UInt32 pNumber);
public void Executecommand(out UInt32 pNumber, out int Res)
{
Res = command(out pNumber);
}
不起作用的函數有兩個參數:1. out pData:PChar 2. const pDataLen(這是返回的數據長度)
VB DLL手冊指出,重要的是在調用函數之前保留內存,並在函數完成后釋放內存。 20個字符就足夠了。
有人可以提示我如何調用該函數嗎? 當我簡單地使用“字符串”時,我得到一個理解:發生了System.AccesViolationException類型的未處理的異常。 其他信息:嘗試讀取或寫入受保護的內存。 這通常表明其他內存已損壞。
感謝幫助。
您嘗試使用指針了嗎?
public static extern void NotWorkFunction(out IntPtr pOut, int someConst)
void Main(string[] args)
{
IntPtr pOut;
int dataLength;
string foo;
NotWorkFunction(pOut, dataLength);
foo = Marshal.PtrToStringAuto(pOut);
BlockFree(pOut);
}
解決方案是使用“引用字符串”而不是“輸出字符串”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.