在WindowsForms中调用dll的方法
我有一个包含此函数的 dll:
int __stdcall PrnText(char *printtext);
在 Windows 窗体中,我有此代码来调用 dll:
[DllImport("Printing.dll", EntryPoint = "PrnText", CharSet = CharSet.Ansi)]
public static extern int PrnText(char *printtext);
当我在 C# 代码中调用该函数时,我收到如下错误:“无法将字符串转换为 char*
PrnText("Hello World");
我应该为 PrnText 提供什么参数() 使其工作?
稍后编辑:
Parameter: printtext
pointer to string containing text to be printed
I have a dll which contains this function:
int __stdcall PrnText(char *printtext);
In Windows Forms i have this code to invoke the dll:
[DllImport("Printing.dll", EntryPoint = "PrnText", CharSet = CharSet.Ansi)]
public static extern int PrnText(char *printtext);
When i call the function in C# code i get an error like this : " cannot cast string to char*
PrnText("Hello World");
What parameter should i give to PrnText() to make it work?
Later edit:
Parameter: printtext
pointer to string containing text to be printed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CLR 知道如何在运行时将
string
转换为非托管char*
。您应该使用接受字符串
的签名,如下所示:请注意,仅当仅输入参数时这才有效。
The CLR knows how to convert a
string
to an unmanagedchar*
at runtime. You should use a signature which accepts astring
, as such:Note that this will work only if the parameter is input only.