Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a dll that is written in c++. And I am p/invoking to call the functions.

I have this c++ declaration.

1-
C#
__declspec(dllexport) bool dll_registerAccount(bool spot, unsigned char* Key,int Length, unsigned char* Source, unsigned char* Target);


2-

C#
__declspec(dllexport) unsigned char MD5(unsigned char *InPut,uint4 Input_Length,unsigned char *OutPut);



I have done this dllimport declaration:

1-
C#
[DllImport("nvsreg.dll", CallingConvention = CallingConvention.Cdecl)]

public static extern bool dll_registerAccount(bool spot, [MarshalAs(UnmanagedType.LPStr)]string Key, int Length, [MarshalAs(UnmanagedType.LPStr)]string Source, [MarshalAs(UnmanagedType.LPStr)]string Target);


2-
C#
[DllImport("nvsreg.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern string MD5([MarshalAs(UnmanagedType.LPStr)]string InPut,long Input_Length, [MarshalAs(UnmanagedType.LPStr)]string OutPut);




Would my DllImport be the equivalent to the c++ ?

please tell me what would be dllimport declaration.

Many thanks for any advice,
Posted
Comments
Sergey Alexandrovich Kryukov 9-Oct-12 10:28am    
At first glance it's correct, so, what's the question? Would it be equivalent? It's supposed to be able to call your unmanaged functions, that's it. Does it work or not?
--SA
sujeet101 10-Oct-12 1:17am    
its not working :(
Sergey Alexandrovich Kryukov 10-Oct-12 2:12am    
I see -- Solution 1 shows a bug which I did not notice at first glance :-)
Does it work after a fix? If it does, please accept Solution 1 (green button).
If not, please comment.
--SA

1 solution

You declared (in the C# side) that the calling convention is Cdecl. This is the default calling convention for C++ but, you can also declare it using the __cdecl keyword (in the C++ side).


The actual entry-point's (for C++ functions) name is different than the function's name, in order to enable Function Overloading. If you want the entry-point's name to be as same as the function's name you can declare the function with the extern "C" keyword.


Something like:

C++
extern "C" __declspec(dllexport) bool __cdecl dll_registerAccount(bool spot, unsigned char* Key,int Length, unsigned char* Source, unsigned char* Target);


Another issue: The second function (MD5) is declared to return unsigned char which is not a string in the C# side...

 
Share this answer
 
Comments
Andrewpeter 9-Oct-12 10:53am    
My vote is 5 for you.
Shmuel Zang 9-Oct-12 12:00pm    
Thanks.
Sergey Alexandrovich Kryukov 10-Oct-12 2:11am    
Good catch -- I just did not notice is from the first glance. My 5.
Respect,
--SA
Shmuel Zang 10-Oct-12 3:33am    
Thank you Sergey.
sujeet101 10-Oct-12 2:33am    
its working now thx .

what would be the dllimport declaration in c# for second function(MD5) ,(how to declare return type of unsigned char )

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900