-
April 17th, 2014, 10:14 AM
#1
Converting C to C# - question about typedef voids
In converting some class code from C to C#, I have come across MANY typedef void *tConnHandle (and other such variables) in the header files. My question is, how do I utilize this in my C# code? What is type void? How can I create a connection handle, for example, when I don't know the data type? If I attempt:
tConnHandle conn = new CreateConnhandle();
or for a new array, called tLocator, how can I instantiate the variable? If I attempt:
tLocator[] lLocator = new tLocator[10];
I get the error: 'the type or namespace 'tLocator' [or tConnHandle] could not be found. Are you missing a using directive or an assembly reference?'
How SHOULD I reference the 'datatype'? Is void the datatype?
-
April 17th, 2014, 11:52 AM
#2
Re: Converting C to C# - question about typedef voids
What do tConnHandle and tLocator represent?
-
April 17th, 2014, 12:53 PM
#3
Re: Converting C to C# - question about typedef voids
Based on the little documentation there is, it looks as if tConnhandle represents a connection handle to the database I need to connect to. And tLocator looks to represent an array, because it is supposed to bring back a number of locator results. I don't understand why tLocator isn't explicitly set up as an array. All I see is 'typedef void *tLocator'. Yet the documentation states that tLocator can hold multiple locators, so I assume its an array, because what else would it be. But how do I use it when I only get errors in my attempts?
-
April 17th, 2014, 02:16 PM
#4
Re: Converting C to C# - question about typedef voids
Originally Posted by CodeBugger
Based on the little documentation there is, it looks as if tConnhandle represents a connection handle to the database I need to connect to. And tLocator looks to represent an array, because it is supposed to bring back a number of locator results. I don't understand why tLocator isn't explicitly set up as an array. All I see is 'typedef void *tLocator'. Yet the documentation states that tLocator can hold multiple locators, so I assume its an array, because what else would it be. But how do I use it when I only get errors in my attempts?
Can you call the dll from a C/C++ program and get it to work. If so, you'll need to create the same structures in C# and look in PInvoke.net for examples of passing arrays (if the dll function needs to have an array passed).
You'll need to look at your function prototypes and write corresponding wrappers. For more help, do a bing search for "how to autogenerate a C# pinvoke wrapper around a c dll".
Another option is to use a generator such as SWIG. http://www.swig.org/
I'd avoid it if you can because the wrapper it produces is ugly and breaks all sorts of C# conventions. However, maybe you can look at the generated code and understand what you need to do.
-
April 17th, 2014, 02:37 PM
#5
Re: Converting C to C# - question about typedef voids
I will certainly look into PInvoke.net as I am able to call a demo program, which is written in C/C++, and get it to work. I have been trying to take the demo and convert it into C# without luck.
Is it typical in C to create variables with the typedef void *_____? Am I right to assume that tLocator is an array, simply based on what the documentation seems to state? I feel like I'm doing a lot of assuming.
That being said, also, in PInvoke, it seems like I need to use MarshalAs, and I would need to know what data type I am using. Should I just assume that tLocator is an array? And what datatype is a connectionhandle? An int?
-
April 17th, 2014, 04:45 PM
#6
Re: Converting C to C# - question about typedef voids
Originally Posted by CodeBugger
I will certainly look into PInvoke.net as I am able to call a demo program, which is written in C/C++, and get it to work. I have been trying to take the demo and convert it into C# without luck.
Is it typical in C to create variables with the typedef void *_____? Am I right to assume that tLocator is an array, simply based on what the documentation seems to state? I feel like I'm doing a lot of assuming.
Don't assume anything. If you have the source code for the demo program, look through it and see what types are passed to the dll functions.
Once you understand what is being passed (for example a pointer to an array of tLocation structures), then look in PInvoke.Net for examples of how to define a structure and prototype a method to pass an array of structures.
-
April 21st, 2014, 01:57 PM
#7
Re: Converting C to C# - question about typedef voids
This thread may help explain structures and PInvoke().
http://forums.codeguru.com/showthrea...hlight=Pinvoke
Can you post a function prototype from one of your C header files?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|