Click here to Skip to main content
15,913,758 members
Home / Discussions / C#
   

C#

 
GeneralRe: Using SHFILEINFO to retrieve Special Folder Icons Pin
Heath Stewart31-Jan-05 13:48
protectorHeath Stewart31-Jan-05 13:48 
Generalevent invocation Pin
cchere31-Jan-05 12:59
cchere31-Jan-05 12:59 
GeneralRe: event invocation Pin
Heath Stewart31-Jan-05 13:28
protectorHeath Stewart31-Jan-05 13:28 
GeneralRe: event invocation Pin
cchere31-Jan-05 18:05
cchere31-Jan-05 18:05 
GeneralRe: event invocation Pin
Heath Stewart1-Feb-05 6:10
protectorHeath Stewart1-Feb-05 6:10 
GeneralRe: event invocation Pin
cchere4-Feb-05 4:34
cchere4-Feb-05 4:34 
Generalcalling C++ dll function in C# Pin
isamir31-Jan-05 12:25
isamir31-Jan-05 12:25 
GeneralRe: calling C++ dll function in C# Pin
Heath Stewart31-Jan-05 13:14
protectorHeath Stewart31-Jan-05 13:14 
Never hard-code a path to a DLL in your DllImportAttribute. Add the DLL to your PATH environment variable or to the current application directory. By doing what you're doing you limit where your application can be installed. Users may not even map their C: drive in Windows NT. Utilize the features of the OS correctly.

An IntPtr doesn't necessary mean the same as an int* in native code. An IntPtr is also processor-dependent, being 32 bits on a 32-bit CPU and 64-bits on a 64-bit CPU.

Depending on whether or not your last two parameters are in/out or just out, use the keyword ref or out respectively.

If those are in/out - and they would appear to be judging by the exception - you are actually trying to pass the address of NULL. That's an access violation in native code, which yields a NullReferenceException in managed code. Why, because the address of NULL (or 0) is not a reference to anything.

So, it would appear the correct declaration of your function is:
[DllImportAttribute("cmd_dll.dll", ExactSpelling=true)]
static extern float CMD_RD(int port, int address, ref err_type, ref err_attr);
The rest of the fields of DllImportAttribute aren't necessary. CharSet, for example, is only necessary when you take a string as a parameter. You only use SetLastError if your function actually uses the SetLastError native API, etc. Read the documentation for the DllImportAttribute for more details.

Also - never export a native function publicly. Encapsulate native functionality for 2 reasons: 1) to provide a level of abstraction so as to control the conditions and parameters of when a function can be called, and 2) because malicious code could use your method to execute arbitrary commands if your assembly asserts P/Invoke permissions.

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

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles] [My Blog]
GeneralRotate Flip Image isn't working Pin
adonisv31-Jan-05 12:17
adonisv31-Jan-05 12:17 
GeneralRe: Rotate Flip Image isn't working Pin
Charlie Williams31-Jan-05 12:32
Charlie Williams31-Jan-05 12:32 
GeneralAhhhhhh Sank you sahh! Pin
adonisv31-Jan-05 16:11
adonisv31-Jan-05 16:11 
GeneralProgram crashes.... Pin
Christian Graus31-Jan-05 12:13
protectorChristian Graus31-Jan-05 12:13 
GeneralRe: Program crashes.... Pin
Heath Stewart31-Jan-05 13:19
protectorHeath Stewart31-Jan-05 13:19 
GeneralRe: Program crashes.... Pin
Christian Graus31-Jan-05 13:27
protectorChristian Graus31-Jan-05 13:27 
GeneralRe: Program crashes.... Pin
Heath Stewart31-Jan-05 13:36
protectorHeath Stewart31-Jan-05 13:36 
GeneralRe: Program crashes.... Pin
Christian Graus31-Jan-05 13:50
protectorChristian Graus31-Jan-05 13:50 
GeneralRe: Program crashes.... Pin
Heath Stewart31-Jan-05 13:56
protectorHeath Stewart31-Jan-05 13:56 
GeneralRe: Program crashes.... Pin
Christian Graus31-Jan-05 14:03
protectorChristian Graus31-Jan-05 14:03 
GeneralRe: Program crashes.... Pin
Heath Stewart1-Feb-05 6:20
protectorHeath Stewart1-Feb-05 6:20 
GeneralRe: Program crashes.... Pin
Christian Graus1-Feb-05 9:05
protectorChristian Graus1-Feb-05 9:05 
GeneralCompare a mask to a filename or path Pin
Guinness4Strength31-Jan-05 12:01
Guinness4Strength31-Jan-05 12:01 
GeneralRe: Compare a mask to a filename or path Pin
Christian Graus31-Jan-05 12:15
protectorChristian Graus31-Jan-05 12:15 
GeneralIs there something better than this... Pin
Member 9631-Jan-05 10:40
Member 9631-Jan-05 10:40 
GeneralRe: Is there something better than this... Pin
Christian Graus31-Jan-05 11:24
protectorChristian Graus31-Jan-05 11:24 
GeneralRe: Is there something better than this... Pin
Member 9631-Jan-05 12:21
Member 9631-Jan-05 12:21 

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.