c# dll调用没有传递char*给c++ MFC常规dll
本文关键字:dll c++ MFC 常规 char 调用 | 更新日期: 2023-09-27 18:05:31
我有一个c++ MFC常规DLL,我用以下方式调用:
public static class Access3rdPartyDLL
{
public static string FilePath;
[DllImport("3rdparty.dll")]
// I have also tried LPWStr
public static extern long Download([MarshalAs(UnmanagedType.LPStr)]string sDownloadFile,
int iDeviceNum
...);
public static long DownloadToDevice()
{
long result;
string FilePath = "C:''myfile.txt"
result = Download(FilePath, 1, ...);
// check if success or error
if(result > 0)
...
}
}
我从DLL中得到一个错误,说"文件:'C:'myfile.txt'未找到。但它就在那里…
我也尝试过使用StringBuilder
,但这也失败了。
这可能是DLL的问题还是我做错了什么?
我在这里找到了当前的代码:SO: c#中的等效char*
编辑:我以前在c++中这样做过,下面的代码可以工作:
extern "C" __declspec(dllimport) HRESULT __stdcall Download(char* sDownloadFile, int ...
,我用:
HRESULT result = Download(file_for_download, 1, .. // where file_for_download is a char*
p/调用的唯一错误是您使用的是64位的c# long
,但HRESULT
只有32位。
您有匹配的调用约定,托管string
的默认封送是非托管端的char*
。
不匹配的返回值大小不能解释为什么你的c#代码接收到一个字符串消息File: 'C:'myfile.txt' not found
,所以你的主要问题很可能在于你没有向我们展示的代码。
我看不出有什么理由不能在这个简单的场景中工作:
[DllImport( "3rdparty.dll", CharSet = CharSet.Ansi )]
static extern long Download(string sDownloadFile, int iDeviceNum, ...)
long result = Download("C:''myfile.txt", 1, ...);