Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke method for dll in WindowsForms

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
like image 603
Emil Dumbazu Avatar asked Jun 19 '26 05:06

Emil Dumbazu


1 Answers

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.

like image 194
Rotem Avatar answered Jun 20 '26 19:06

Rotem