Click here to Skip to main content
15,890,512 members
Home / Discussions / C#
   

C#

 
GeneralXmodem implimentation Pin
therealmccoy4-Jan-05 12:35
therealmccoy4-Jan-05 12:35 
GeneralRe: Xmodem implimentation Pin
Heath Stewart4-Jan-05 13:37
protectorHeath Stewart4-Jan-05 13:37 
GeneralRe: Xmodem implimentation Pin
therealmccoy4-Jan-05 14:29
therealmccoy4-Jan-05 14:29 
GeneralRe: Xmodem implimentation Pin
Heath Stewart4-Jan-05 20:12
protectorHeath Stewart4-Jan-05 20:12 
GeneralString output in C# from a C++ dll Pin
vinayakdl4-Jan-05 12:29
vinayakdl4-Jan-05 12:29 
GeneralRe: String output in C# from a C++ dll Pin
Heath Stewart4-Jan-05 13:34
protectorHeath Stewart4-Jan-05 13:34 
GeneralRe: String output in C# from a C++ dll Pin
vinayakdl5-Jan-05 4:53
vinayakdl5-Jan-05 4:53 
GeneralRe: String output in C# from a C++ dll Pin
Heath Stewart5-Jan-05 7:04
protectorHeath Stewart5-Jan-05 7:04 
I see many problems with that.

First, never hard-code the path to your DLL from which you want to import a function. Just set the DLL name and make sure the directory is in your PATH environment variable or in the local directory of your application that runs this code (not necessarily where the DLL assembly that may contain this code is). This is how Windows (and actually other OS's like linux and UNIX) finds DLLs. Read the documentation for the native CreateProcess API for more information.

Seconds, you're setting CharSet to CharSet.Unicode. char* is an ANSI string. Be sure to read the documentation for the CharSet enumeration for details.

Third, you don't need much of the fields you're setting in the DllImportAttribute. You only need exact spelling if, for example, you're importing SomeFunctionA or SomeFunctionW, where you don't want an "A" or "W" appended depending on the CharSet. It won't hurt, though. And only set SetLastError to true if your function actually sets the last error. I see nothing in your code that would.

The simple signature for your native function follows:
[DllImport("qw.dll")]
static extern string ReportVersion(string a);
However, there are a few reasons why this won't work and a few things you should be aware of.

A char* is, obviously, a pointer to a char. If you're setting this to another string the pointer is is modified to point to another string such that when your function returns what you passed as the parameter is the new string. You don't need to return it as well. Besides, you should never return a string because it's not possible to return an error code if the function fails. You're only choice is to return NULL, which doesn't tell you what actually failed. You could use SetLastError in your native code, but it's far better to return an error value.

Also, you're importing a method - not a function. Methods use the CallingConvention.ThisCall - at least instance methods. Now you have a problem.

You can't call your instance method without a context to whatever this would be in your native code (an instance of CQwApp)

This means you have to set the DllImportAttibute.CallingConvention field to CallingConvention.ThisCall and create a pair of proxy functions (not methods) to create and destroy (or just a single proxy method to get) an instance of CQwApp. See a previous reply regarding this issue at http://www.codeproject.com/script/comments/forums.asp?msg=771919&forumid=1649#xx771919xx[^].

Finally, you should consider that - if your method really is so basic and doesn't require instance data - either define it as static and use the __cdecl calling convention (which means you don't declare any DllImportAttribute.CallingConvention, though you may need to set the EntryPoint explicitly since the name will be C++ mangled) or declare it as a function instead of any sort of method. That will make it easier to P/Invoke.

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]
GeneralRe: String output in C# from a C++ dll Pin
vinayakdl6-Jan-05 9:55
vinayakdl6-Jan-05 9:55 
GeneralRe: String output in C# from a C++ dll Pin
Heath Stewart6-Jan-05 12:04
protectorHeath Stewart6-Jan-05 12:04 
GeneralHELP! Web secuirty ... Pin
mrwonkothesane424-Jan-05 11:38
mrwonkothesane424-Jan-05 11:38 
GeneralReplacing text Pin
Christer Claesson4-Jan-05 11:35
Christer Claesson4-Jan-05 11:35 
GeneralSpecial Keyboard keys Pin
BertGo4-Jan-05 10:25
BertGo4-Jan-05 10:25 
GeneralRe: Special Keyboard keys Pin
Heath Stewart4-Jan-05 11:12
protectorHeath Stewart4-Jan-05 11:12 
GeneralRe: Special Keyboard keys Pin
BertGo4-Jan-05 13:00
BertGo4-Jan-05 13:00 
GeneralRe: Special Keyboard keys Pin
Heath Stewart4-Jan-05 13:29
protectorHeath Stewart4-Jan-05 13:29 
Generalstopping service via WMI Pin
thespiff4-Jan-05 8:36
thespiff4-Jan-05 8:36 
GeneralRe: stopping service via WMI Pin
Heath Stewart4-Jan-05 10:51
protectorHeath Stewart4-Jan-05 10:51 
GeneralRe: stopping service via WMI Pin
thespiff5-Jan-05 1:03
thespiff5-Jan-05 1:03 
GeneralRe: stopping service via WMI Pin
Heath Stewart5-Jan-05 4:50
protectorHeath Stewart5-Jan-05 4:50 
GeneralRe: stopping service via WMI Pin
thespiff6-Jan-05 2:19
thespiff6-Jan-05 2:19 
GeneralImplementing mouse panning functionality Pin
Andres Coder4-Jan-05 7:55
Andres Coder4-Jan-05 7:55 
GeneralRe: Implementing mouse panning functionality Pin
Heath Stewart4-Jan-05 11:30
protectorHeath Stewart4-Jan-05 11:30 
GeneralIs this acceptable practice Pin
Wayne Phipps4-Jan-05 7:54
Wayne Phipps4-Jan-05 7:54 
GeneralRe: Is this acceptable practice Pin
perlmunger4-Jan-05 8:41
perlmunger4-Jan-05 8:41 

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.