I'm using a DLL with the following function in C# Visual Studio 2008:
[DllImport("slpapi62", EntryPoint = "_SlpGetPrinterName@12")]
public static extern int SlpGetPrinterName(int nIndex, string strPrinterName, int nMaxChars);
Calling this function, strPrinterName
is suppose to return a string.
string name = "";
SlpGetPrinterName(0, name, 128);
How can i get this parameter to pass by reference?
Pass a StringBuilder
object instead of a string:
[DllImport("slpapi62", EntryPoint = "_SlpGetPrinterName@12")]
public static extern int SlpGetPrinterName(int nIndex, StringBuilder strPrinterName, int nMaxChars);
Calling it:
StringBuilder name = new StringBuilder(128);
int value = SlpGetPrinterName(0, name, name.Capacity);
使用StringBuilder
对象作为string参数。
Can you use the ref keyword?
string name = "";
SlpGetPrinterName(0, ref name, 128);
There is a detail explanation of passing variables by reference here http://www.yoda.arachsys.com/csharp/parameters.html
It sounds like you need the name value to be set by the external code. It has been some time since I have done any pInvoke, but I believe the following is the correct signature:
[DllImport("slpapi62", EntryPoint = "_SlpGetPrinterName@12")]
public static extern int SlpGetPrinterName(int nIndex, out string strPrinterName, int nMaxChars);
Note the 'out' keyword before 'string strPrinterName'. You would then call it like so:
string name;
SlpGetPrinterName(0, out name, 128);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.