Click here to Skip to main content
15,892,737 members
Home / Discussions / C#
   

C#

 
GeneralRe: Nevermind, found the answer Pin
Heath Stewart15-Sep-04 6:59
protectorHeath Stewart15-Sep-04 6:59 
GeneralRe: Nevermind, found the answer Pin
blakeb_117-Sep-04 5:34
blakeb_117-Sep-04 5:34 
GeneralScene change detection doubt Pin
ee9903515-Sep-04 5:55
ee9903515-Sep-04 5:55 
GeneralRe: Scene change detection doubt Pin
Heath Stewart15-Sep-04 6:54
protectorHeath Stewart15-Sep-04 6:54 
GeneralRe: Scene change detection doubt Pin
ee9903515-Sep-04 8:04
ee9903515-Sep-04 8:04 
GeneralRe: Scene change detection doubt Pin
Manajit19-May-10 20:04
Manajit19-May-10 20:04 
GeneralLPWSTR in C# Pin
yyf15-Sep-04 5:41
yyf15-Sep-04 5:41 
GeneralRe: LPWSTR in C# Pin
Heath Stewart15-Sep-04 7:10
protectorHeath Stewart15-Sep-04 7:10 
First, understand how the unmanaged code (i.e., native code) looks (essentially):
typedef char CHAR;
typedef CHAR* LPSTR;
typedef wchar_t WCHAR;
typedef WCHAR* LPWSTR;
#ifdef UNICODE
typedef WCHAR TCHAR;
typedef WCHAR* LPTSTR;
#else
typedef CHAR TCHAR;
typedef CHAR* LPSTR;
#endif
That means that any Unicode strings use "W" as a convention (this is true with most Microsoft APIs - as well as others' APIs - that take strings as parameters; they end with either "A" for ANSI or "W" for Unicode and are #define'd without either "A" or "W", which is the one you typically call).

When you use P/Invoke or COM interop (both are interop features), you must dictate which string encoding to use. By default in C#, CharSet.Ansi is used. In most cases when calling Microsoft APIs (and others - those I mentioned above), you will want to use CharSet.Auto. Where do you use those?

When declaring P/Invoke methods, you use them in the DllImportAttribute like so:
// Note: following is only anexample.
// You can more easily get the computer name by using Environment.MachineName.
[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern bool GetComputerName([Out] string name, ref int size);
When you're declaring a struct with string members, you do the same in the StructLayoutAttribute.

In both cases, you can override the string encoding using the MarshalAsAttribute.

Lets say, for some reason, you had a struct with ANSI and Unicode versions, except for one string member that must always be ANSI:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct Example
{
  public string Field1;
  public string Field2;
  [MarshalAs(UnmanagedType.LPStr)] public string Field3;
}
We default to CharSet.Auto (saves typing and is easier to maintain) but override the unmanaged type as UnmanagedType.LPStr (as opposed to UnmanagedType.LPWStr), so that it's always ANSI. You can do the same with P/Invoke declarations.

Just make sure you understand the unmanaged code well enough to know whether it's always ANSI or Unicode, or if it is platform dependent (i.e., ANSI on Windows, Unicode on Windows NT).

This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles]
GeneralRe: LPWSTR in C# Pin
yyf15-Sep-04 9:43
yyf15-Sep-04 9:43 
GeneralRe: LPWSTR in C# Pin
Heath Stewart15-Sep-04 10:16
protectorHeath Stewart15-Sep-04 10:16 
GeneralRe: How to change the text of a text object at runtime in crystal reports for .NET (C#) Pin
Dave Kreskowiak15-Sep-04 3:56
mveDave Kreskowiak15-Sep-04 3:56 
GeneralRe: wall on this one:simple string search question Pin
sreejith ss nair15-Sep-04 3:46
sreejith ss nair15-Sep-04 3:46 
GeneralRe: wall on this one:simple string search question Pin
Heath Stewart15-Sep-04 7:22
protectorHeath Stewart15-Sep-04 7:22 
Generalsimple string search question Pin
Vodstok15-Sep-04 3:32
Vodstok15-Sep-04 3:32 
GeneralRe: simple string search question Pin
sreejith ss nair15-Sep-04 3:44
sreejith ss nair15-Sep-04 3:44 
GeneralRe: simple string search question Pin
Vodstok15-Sep-04 3:58
Vodstok15-Sep-04 3:58 
GeneralRe: simple string search question Pin
sreejith ss nair15-Sep-04 4:15
sreejith ss nair15-Sep-04 4:15 
GeneralRe: simple string search question Pin
sreejith ss nair15-Sep-04 4:28
sreejith ss nair15-Sep-04 4:28 
Generalwall on this one:simple string search question Pin
Vodstok15-Sep-04 3:27
Vodstok15-Sep-04 3:27 
GeneralPopulate TreeView with ArrayList Pin
93Current15-Sep-04 2:31
93Current15-Sep-04 2:31 
GeneralRe: Populate TreeView with ArrayList Pin
Nnamdi Onyeyiri15-Sep-04 3:22
Nnamdi Onyeyiri15-Sep-04 3:22 
GeneralRe: Populate TreeView with ArrayList Pin
93Current15-Sep-04 8:11
93Current15-Sep-04 8:11 
Generalfinding Movie duration of a wmv file Pin
gupta vaibhav15-Sep-04 0:04
gupta vaibhav15-Sep-04 0:04 
GeneralRe: finding Movie duration of a wmv file Pin
sreejith ss nair15-Sep-04 0:37
sreejith ss nair15-Sep-04 0:37 
GeneralRe: finding Movie duration of a wmv file Pin
gupta vaibhav15-Sep-04 3:39
gupta vaibhav15-Sep-04 3:39 

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.