Click here to Skip to main content
15,891,951 members
Home / Discussions / C#
   

C#

 
GeneralRe: Multiline textbox down arrow is missing Pin
Sudhakar Pasupunuri15-Jun-04 3:41
Sudhakar Pasupunuri15-Jun-04 3:41 
GeneralRe: Multiline textbox down arrow is missing Pin
Heath Stewart15-Jun-04 4:01
protectorHeath Stewart15-Jun-04 4:01 
GeneralRe: Multiline textbox down arrow is missing Pin
Wackatronic15-Jun-04 9:26
Wackatronic15-Jun-04 9:26 
GeneralRe: Multiline textbox down arrow is missing Pin
Sudhakar Pasupunuri15-Jun-04 20:01
Sudhakar Pasupunuri15-Jun-04 20:01 
GeneralDatabinding objects Pin
WillemM14-Jun-04 21:40
WillemM14-Jun-04 21:40 
GeneralRe: Databinding objects Pin
Heath Stewart15-Jun-04 3:19
protectorHeath Stewart15-Jun-04 3:19 
GeneralNULLReferenceException Pin
qur14-Jun-04 21:39
qur14-Jun-04 21:39 
GeneralRe: NULLReferenceException Pin
Heath Stewart15-Jun-04 3:15
protectorHeath Stewart15-Jun-04 3:15 
Don't ref the string. A char* is a string. The signature in C# should be (and loose the path - put the DLL in a directory in your PATH environment variable, or the current application directory):
[DllImport("MyFunctions.dll", CharSet=CharSet.Ansi)]
public static extern int GetName(int nProb, int nIndex, string sVarName);
Also notice the CharSet field in the DllImportAttribute. .NET deals with Unicode strings. If you don't specify a CharSet, CharSet.Ansi is used by default for C# and VB.NET, but it's still a good idea to specify it for readability.

Also notice there's no out or ref keyword for the sVarName parameter (which, by the way, can be called anything you want). A String (C# alias string) is already a reference type.

Also, if you want your code to be portable, those int parameters and return should actually be IntPtr. An int in C/C++ is a processor-dependent bit width (i.e., 32 bits on a 32-bit CPU, 64 bits on a 64-bit CPU). int in .NET (an Int32, actually) is always 32 bits. When .NET 2.0 is released and if your code runs on a 64-bit platform (and your native DLL is recompiled for a 64-bit platform), your call will fail.

Finally, never make P/Invoke'd methods public (unless the class that declares them is private or internal). Native function calls are unmanaged, so the CLR can't determine if someone would exploit your code. Depending on your native implementation, someone could easily perform a buffer overrun using the string variable and the CLR couldn't prevent it because it's unmanaged. P/Invoke'd methods are typically wrapped in a class that encapsulates the functionality that you're trying to use in a native DLL. Many classes throughout the .NET Framework Class Library (FCL) - such as every Control in Windows Forms - does this.

 

Microsoft MVP, Visual C#
My Articles
Generalhelp! java webservices and C# clients... Pin
Nik Vogiatzis14-Jun-04 20:56
Nik Vogiatzis14-Jun-04 20:56 
GeneralRe: help! java webservices and C# clients... Pin
Heath Stewart15-Jun-04 3:08
protectorHeath Stewart15-Jun-04 3:08 
GeneralRe: help! java webservices and C# clients... Pin
Nik Vogiatzis15-Jun-04 3:14
Nik Vogiatzis15-Jun-04 3:14 
GeneralRe: help! java webservices and C# clients... Pin
Heath Stewart15-Jun-04 3:57
protectorHeath Stewart15-Jun-04 3:57 
GeneralRe: help! java webservices and C# clients... Pin
Nik Vogiatzis15-Jun-04 12:12
Nik Vogiatzis15-Jun-04 12:12 
GeneralHTTP Requests and Posts Pin
Richard Beacroft14-Jun-04 20:40
Richard Beacroft14-Jun-04 20:40 
GeneralRe: HTTP Requests and Posts Pin
Heath Stewart15-Jun-04 3:04
protectorHeath Stewart15-Jun-04 3:04 
GeneralCompile my C# Class Pin
dabuskol14-Jun-04 20:33
dabuskol14-Jun-04 20:33 
GeneralRe: Compile my C# Class Pin
Tarakeshwar Reddy14-Jun-04 23:04
professionalTarakeshwar Reddy14-Jun-04 23:04 
GeneralRe: Compile my C# Class Pin
dabuskol14-Jun-04 23:44
dabuskol14-Jun-04 23:44 
GeneralRe: Compile my C# Class Pin
Heath Stewart15-Jun-04 2:57
protectorHeath Stewart15-Jun-04 2:57 
GeneralRe: Compile my C# Class Pin
dabuskol15-Jun-04 19:00
dabuskol15-Jun-04 19:00 
Generalabout UITypeEditor Operation in RunTime Pin
pig123414-Jun-04 17:47
pig123414-Jun-04 17:47 
GeneralRe: about UITypeEditor Operation in RunTime Pin
Heath Stewart15-Jun-04 2:54
protectorHeath Stewart15-Jun-04 2:54 
GeneralRe: 0 result with double data type Pin
myNameIsRon14-Jun-04 17:25
myNameIsRon14-Jun-04 17:25 
GeneralRe: 0 result with double data type Pin
Colin Angus Mackay14-Jun-04 23:36
Colin Angus Mackay14-Jun-04 23:36 
GeneralRe: 0 result with double data type Pin
myNameIsRon15-Jun-04 20:05
myNameIsRon15-Jun-04 20:05 

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.