在WindowsForms中调用dll的方法

发布于 2024-12-29 11:40:31 字数 526 浏览 3 评论 0原文

我有一个包含此函数的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昨迟人 2025-01-05 11:40:31

CLR 知道如何在运行时将string 转换为非托管char*。您应该使用接受字符串的签名,如下所示:

public static extern int PrnText(string printtext);

请注意,仅当仅输入参数时这才有效。

The CLR knows how to convert a string to an unmanaged char* at runtime. You should use a signature which accepts a string, as such:

public static extern int PrnText(string printtext);

Note that this will work only if the parameter is input only.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文