I'm using a DLL with the following function in C# Visual Studio 2008:
我在C#Visual Studio 2008中使用具有以下功能的DLL:
[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.
调用此函数,strPrinterName假定返回一个字符串。
string name = "";
SlpGetPrinterName(0, name, 128);
How can i get this parameter to pass by reference?
如何通过引用传递此参数?
4 个解决方案
#1
Pass a StringBuilder
object instead of a string:
传递StringBuilder对象而不是字符串:
[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);
#2
Use a StringBuilder
object for the string parameter.
对String参数使用StringBuilder对象。
#3
Can you use the ref keyword?
你能用ref关键字吗?
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
有关传递变量的详细说明,请参见http://www.yoda.arachsys.com/csharp/parameters.html
#4
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:
听起来你需要通过外部代码设置名称值。我做了任何pInvoke已经有一段时间了,但我相信以下是正确的签名:
[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 strPrinterName'之前的'out'关键字。然后你会这样称呼它:
string name;
SlpGetPrinterName(0, out name, 128);
#1
Pass a StringBuilder
object instead of a string:
传递StringBuilder对象而不是字符串:
[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);
#2
Use a StringBuilder
object for the string parameter.
对String参数使用StringBuilder对象。
#3
Can you use the ref keyword?
你能用ref关键字吗?
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
有关传递变量的详细说明,请参见http://www.yoda.arachsys.com/csharp/parameters.html
#4
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:
听起来你需要通过外部代码设置名称值。我做了任何pInvoke已经有一段时间了,但我相信以下是正确的签名:
[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 strPrinterName'之前的'out'关键字。然后你会这样称呼它:
string name;
SlpGetPrinterName(0, out name, 128);