Click here to Skip to main content
15,070,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.
I Have A C dll That I Got From Another team in My project.
I Try to Load A Certain Function From it in C#
The function looks Like That:
Void GetData(TCHAR name [], char **address, DWORD *size)
How Should I declare The equivilant Function in C# ?
I am Using DllImport...
Thanks
Dj4400
Posted
Comments
zlogdan 28-May-13 13:49pm
   
I have had to marshal C code into C# in the past, it was not easy, but I left this question at this forum and it helped me: http://stackoverflow.com/questions/3264789/how-to-convert-a-void-to-a-type-that-can-be-used-in-c-interoperability-betwee

Using unsafe and char*.
The Function looks Like This:
public static extern void GetData(stringBuilder str, char **addr, uint *size)
   
I use to use this way call functions with pointer to pointer arguments. This may good for your function also.

The string and the string list would be used with int pointers:
C#
public static extern void GetData( System.IntPtr name, System.IntPtr address, ref Int32 Size)


before call alloc name:

C#
IntPtr name = Marshal.AllocHGlobal(Marshal.SystemDefaultCharSize * <maxsize>);


then call:
C#
GetData(ref name, ref address, ref Size);


you can get the Name and Address like this way:

C#
string Name = GlobalMarshaller.StringFromIntPtr(name);
string[] Adress = GlobalMarshaller.StringFromIntPtr(address, <maxcount>, <maxsize>);

Then clean up!
C#
Marshal.FreeHGlobal(name);
Marshal.FreeHGlobal(adress);
   
Comments
dj4400 2-Jun-13 9:34am
   
This Function Gets The name Patameter With A value ("Dllname.dll"), The address Parameter is Passed For allocation in The function. The Size Of
The Allocation is Returned In size parameter. I dont Think AllocHGlobal Is relevant. I Still Dont Know how To Declate It Without Unsafe...
Hi dj4400u,

Use

public static extern void GetData(stringbuilder sb,string,double);

Also make sure that your dll path is right.make it hardcoded path first and then try with runtime path.

Please let me know in case of any issue.

Thanks
sjs
   
Comments
Noel Jo 27-May-13 5:58am
   
yeah!
dj4400 27-May-13 8:54am
   
Its not working.... I Get An Acess Violation Exception
Attempted to Read or Write Protected Memory.
When Using The dll in c++ App i Declare:
TCHAR Str[260] And copy A string to it With strcpy
Char *addr[64] And DWORD Size[64]
The Call to the Dll Function Looks Like This:
GetData(Str ,&addr[i], &Size[i]);
Both Addr And size Are Global And dont Get A Value Before The Function Call
dj4400 27-May-13 10:58am
   
The dll function itself returns Addr Allocated and a Number In Size.
If i put a Break Point The Values That are send to the function
Are A "String" and 2 addresses The First One Points To A 'bad Pointer'
dj4400 27-May-13 10:58am
   
The second one points to zero
Richard MacCutchan 28-May-13 3:39am
   
How do you get a double from a DWORD*, or a string from a 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