DLLImport issues

J

Jeremy Chapman

The following is the original c++ header from wtsapi32.h followed by my
DLLImport syntax so that I can call the function from a c# application. Is
my DLL Import syntax correct? Every call to the function seems to return
false, with nothing in strBuffer or lBytesReturned.


/*BOOL WINAPI WTSQueryUserConfigA(
IN LPSTR pServerName,
IN LPSTR pUserName,
IN WTS_CONFIG_CLASS WTSConfigClass,
OUT LPSTR * ppBuffer,
OUT DWORD * pBytesReturned);*/

public enum enWTSClass
{
//Initial program settings
WTSUserConfigInitialProgram = 0x0, // string returned/expected
WTSUserConfigWorkingDirectory = 0x1, // string
returned/expected
WTSUserConfigfInheritInitialProgram = 0x2, // DWORD
returned/expected
//
WTSUserConfigfAllowLogonTerminalServer = 0x3, //DWORD returned/expected
//Timeout settings
WTSUserConfigTimeoutSettingsConnections = 0x4, //DWORD returned/expected
WTSUserConfigTimeoutSettingsDisconnections = 0x5, //DWORD returned/expected
WTSUserConfigTimeoutSettingsIdle = 0x6, //DWORD returned/expected
//Client device settings
WTSUserConfigfDeviceClientDrives = 0x7, //DWORD returned/expected
WTSUserConfigfDeviceClientPrinters = 0x8, //DWORD returned/expected
WTSUserConfigfDeviceClientDefaultPrinter = 0x9, //DWORD returned/expected
//Connection settings
WTSUserConfigBrokenTimeoutSettings = 0xA, //DWORD returned/expected
WTSUserConfigReconnectSettings = 0xB, //DWORD returned/expected
//Modem settings
WTSUserConfigModemCallbackSettings = 0xC, //DWORD returned/expected
WTSUserConfigModemCallbackPhoneNumber = 0xD, // string
returned/expected
//Shadow settings
WTSUserConfigShadowingSettings = 0xE, //DWORD returned/expected
//User Profile settings
WTSUserConfigTerminalServerProfilePath = 0xF, // string
returned/expected
//Terminal Server home directory
WTSUserConfigTerminalServerHomeDir = 0x10, // string
returned/expected
WTSUserConfigTerminalServerHomeDirDrive = 0x11, // string
returned/expected
WTSUserConfigfTerminalServerRemoteHomeDir = 0x12 // DWORD 0:LOCAL 1:REMOTE
};

[DllImport("wtsapi32.dll")]
private static extern bool WTSQueryUserConfigA(
string strServerName,
string strUserName,
enWTSClass eWTSConfigClass,
ref string strBuffer,
ref long lBytesReturned);
 
M

Mattias Sjögren

Jeremy,
[DllImport("wtsapi32.dll")]
private static extern bool WTSQueryUserConfigA(
string strServerName,
string strUserName,
enWTSClass eWTSConfigClass,
ref string strBuffer,
ref long lBytesReturned);

Try it like this

[DllImport("wtsapi32.dll", CharSet=CharSet.Auto)]
private static extern bool WTSQueryUserConfig(
string strServerName,
string strUserName,
enWTSClass eWTSConfigClass,
StringBuilder strBuffer,
ref int lBytesReturned);



Mattias
 
N

news.microsoft.com

You dont need CharSet=CharSet.Auto attribute
Mattias Sjögren said:
Jeremy,
[DllImport("wtsapi32.dll")]
private static extern bool WTSQueryUserConfigA(
string strServerName,
string strUserName,
enWTSClass eWTSConfigClass,
ref string strBuffer,
ref long lBytesReturned);

Try it like this

[DllImport("wtsapi32.dll", CharSet=CharSet.Auto)]
private static extern bool WTSQueryUserConfig(
string strServerName,
string strUserName,
enWTSClass eWTSConfigClass,
StringBuilder strBuffer,
ref int lBytesReturned);



Mattias
 
N

news.microsoft.com

According to MSDN it defaults to ANSI, and not Auto. I think this should
default to Auto if omitted. Why would it not be? Has MS got a good reason
for defaulting to Ansi on NT platforms and not Auto? Doesnt sound logical
to me.
 
N

Nick Wienholt

According to MSDN it defaults to ANSI, and not Auto. I think this should
default to Auto if omitted. Why would it not be? Has MS got a good reason
for defaulting to Ansi on NT platforms and not Auto? Doesnt sound logical
to me.


The default of ANSI seems like a bad choice. I vagely remeber some
discussion that Auto wouldn't work for all function exports if there was an
'A' or 'W' version missing, but even of this was the guess, setting CharSet
to the appropriate values in this case would be better than taking the
performance hit all the time if there is a string parameter present.

Nick
 
Top