Click here to Skip to main content
15,896,730 members
Home / Discussions / C#
   

C#

 
GeneralRe: help on wmi and registry Pin
Heath Stewart11-May-04 3:59
protectorHeath Stewart11-May-04 3:59 
GeneralRe: help on wmi and registry Pin
chettu11-May-04 20:18
chettu11-May-04 20:18 
GeneralRe: help on wmi and registry Pin
Heath Stewart12-May-04 2:52
protectorHeath Stewart12-May-04 2:52 
GeneralRe: help on wmi and registry Pin
chettu12-May-04 3:21
chettu12-May-04 3:21 
Generalblock the keyboard Pin
cristina_tudor11-May-04 0:36
cristina_tudor11-May-04 0:36 
GeneralRe: block the keyboard Pin
Corinna John11-May-04 0:50
Corinna John11-May-04 0:50 
GeneralReturning string from unmanaged dll Pin
Mikke_x10-May-04 23:50
Mikke_x10-May-04 23:50 
GeneralRe: Returning string from unmanaged dll Pin
Heath Stewart11-May-04 3:31
protectorHeath Stewart11-May-04 3:31 
If your unmanaged declaration looked like this:
LPCTSTR SomeFunc();
Then your managed declaration should look like this
[DllImport("whatever.dll", CharSet=CharSet.Auto)]
private static extern string SomeFunc();
Notice the CharSet=CharSet.Auto? That marshals the string correctly. Most likely what you're seeing is the result of marshaling an ASCII string as Unicode, or a Unicode string as ASCII. Strings in .NET are all treated as Unicode, so you have to tell the CLR how to marshal strings from unmanaged code. See the CharSet enumeration in the .NET Framework SDK for more information.

The reason some characters are written as ? is because the character representation isn't printable, meaning you got some junk data in there. This could be an indication that something is wrong with your unmanaged function.

You should read Consuming Unmanaged DLL Functions[^] and Interop Marshaling[^] in the .NET Framework SDK. What you're trying to do is easy, but you need to understand the basics first.

BTW, char* or char[] is a string (in this case, an LPSTR, as defined in the Windows headers, but it could be define as anything). When P/Invoking unmanaged APIs, it's better to use a string and marshaling it (also see the MarshalAsAttribute for more assistance and information) rather than a char array. It works the same, but passing a string is much easier than passing a char array.

 

Microsoft MVP, Visual C#
My Articles
GeneralRe: Returning string from unmanaged dll Pin
Mikke_x11-May-04 4:06
Mikke_x11-May-04 4:06 
GeneralRe: Returning string from unmanaged dll Pin
Heath Stewart11-May-04 4:59
protectorHeath Stewart11-May-04 4:59 
GeneralRe: Returning string from unmanaged dll Pin
Mikke_x11-May-04 5:02
Mikke_x11-May-04 5:02 
GeneralRe: Returning string from unmanaged dll Pin
Heath Stewart11-May-04 5:20
protectorHeath Stewart11-May-04 5:20 
QuestionFont to LOGFONT ? Pin
azusakt10-May-04 23:06
azusakt10-May-04 23:06 
AnswerRe: Font to LOGFONT ? Pin
Kannan Kalyanaraman10-May-04 23:22
Kannan Kalyanaraman10-May-04 23:22 
GeneralRe: Font to LOGFONT ? Pin
Heath Stewart11-May-04 3:40
protectorHeath Stewart11-May-04 3:40 
GeneralRe: Font to LOGFONT ? Pin
azusakt11-May-04 16:55
azusakt11-May-04 16:55 
GeneralRe: Font to LOGFONT ? Pin
Heath Stewart12-May-04 2:45
protectorHeath Stewart12-May-04 2:45 
GeneralRe: Font to LOGFONT ? Pin
azusakt11-May-04 22:41
azusakt11-May-04 22:41 
GeneralRe: Font to LOGFONT ? Pin
azusakt12-May-04 15:20
azusakt12-May-04 15:20 
GeneralSockets Pin
bouli10-May-04 22:58
bouli10-May-04 22:58 
GeneralRe: Sockets Pin
Heath Stewart11-May-04 3:44
protectorHeath Stewart11-May-04 3:44 
GeneralQuartz.dll Pin
MeterMan10-May-04 22:51
MeterMan10-May-04 22:51 
GeneralRe: Quartz.dll Pin
Heath Stewart11-May-04 3:46
protectorHeath Stewart11-May-04 3:46 
GeneralString question Pin
pankajdaga10-May-04 22:26
pankajdaga10-May-04 22:26 
GeneralRe: String question Pin
Kannan Kalyanaraman10-May-04 23:07
Kannan Kalyanaraman10-May-04 23:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.