Converting C to C# - question about typedef voids
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Converting C to C# - question about typedef voids

  1. #1
    Join Date
    May 2006
    Posts
    6

    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?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,370

    Re: Converting C to C# - question about typedef voids

    What do tConnHandle and tLocator represent?

  3. #3
    Join Date
    May 2006
    Posts
    6

    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?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,370

    Re: Converting C to C# - question about typedef voids

    Quote Originally Posted by CodeBugger View Post
    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.

  5. #5
    Join Date
    May 2006
    Posts
    6

    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?

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,370

    Re: Converting C to C# - question about typedef voids

    Quote Originally Posted by CodeBugger View Post
    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.

  7. #7
    Join Date
    May 2002
    Posts
    487

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center