首页 / C# / 从C和C#调用C DLL
从C和C#调用C DLL
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了从C和C#调用C DLL,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1430字,纯文字阅读大概需要3分钟。
内容图文
![从C和C#调用C DLL](/upload/InfoBanner/zyjiaocheng/685/f5e03a5fdfe74ea084c27633d3eff15f.jpg)
我有一个C应用程序,我必须将其转换为DLL.我有全部资料.
我的功能是
外部“ C”
__declspec(dllexport)int mymain(int i,std :: wstring myArgs)
我需要能够从c或c#包装器传递参数.我可以从c控制台应用程序调用此程序,而不会出错.我现在正试图从C#中调用它.
这是我的C#代码:
public static class DllHelper
{
[DllImport("rep.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int mymain(int iArgs, string aArgs);
}
class Program
{
static void Main(string[] args)
{
string s = "my string data";
DllHelper.mymain(0, s);
}
}
}
当我运行它时,我得到
System.Runtime.InteropServices.SEHException: ‘External component has thrown an exception.’
我没主意.
TIA
解决方法:
指定Unicode,但也可以在C或C函数中将printf与“%S”一起使用(大写的“ S”表示宽字符字符串).或std :: wcout.
否则,它可能会打印出奇怪的文字或在找到的第一个空字符处终止.另外,您可能希望实际传递字符串的长度,但这完全取决于您.
请注意,C函数的签名对myArgs参数使用LPCWSTR(const wchar_t *).
public static class DllHelper
{
[DllImport("rep.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public static extern int mymain(int iArgs, string aArgs);
}
class Program
{
static void Main(string[] args)
{
string s = "my string data";
DllHelper.mymain(0, s);
}
}
#ifdef __cplusplus
extern "C" {
#endif
int __declspec(dllexport) mymain(int i, const wchar_t* myArgs)
{
#ifdef __cplusplus
std::wcout<<std::wstring(myArgs)<<L"\n";
#else
printf(L"%S\n", myArgs);
#endif
}
#ifdef __cplusplus
}
#endif
内容总结
以上是互联网集市为您收集整理的从C和C#调用C DLL全部内容,希望文章能够帮你解决从C和C#调用C DLL所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。