• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 619
  • Last Modified:

c# wrappers on a c++/visual basic sdk

Hi all,
We have a third party sdk that is written in Microsoft Visual C++ 6.0 but support C++ and Visual Basic.
Unfortunately we are all c# developers, so we cant use the sdk in its current form.
We were advised to create c# wrappers for the sdk, but I have no idea how to go about this.
How do we go about creating a wrapper?
Also if we made a Visual Basic DLL could we use this dll in c#?
Thanks
Dave
0
CoreSupport
Asked:
CoreSupport
  • 4
  • 3
1 Solution
 
AlexFMCommented:
Visual C# has PInvoke feature which allows to  call API functions exported from unmanaged libraries. It works exactly like VB6 API calls (but more flexible because C# is better language). Read about PInvoke in the following article:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp09192002.asp

It describes calling functions from system libraries like kernel32.dll, but the same way can be applied to any dll.

VB^ Class Library can be called from C#, but it is better to call API directly from C#.
0
 
CoreSupportAuthor Commented:
Hey there,
Thanks for the tip! thats great I m going to have a look at that tomorrow
When you say "allows to  call API functions exported from unmanaged libraries" does that mean I have to export the DLL functions myself?
Or can call the api function by just referencing the dll like below?

 [DllImport("example.dll")]
 public static extern Function1 (int x, int y);

Do you know off hand if it is able to call functions that pass custom objects?
e.g.
 [DllImport("example.dll")]
 public static extern Function1 (MessageManager messageManager, int y);

Any help would be great!
Thanks
Dave


0
 
AlexFMCommented:
>> does that mean I have to export the DLL functions myself?

No. It was description for library writer, don't care about this. From client point of view, C# can call any function which can be called from VB6 using API calls. Actually, it can do more because C# is more flexible and can work with pointers.

[DllImport("example.dll")]
public static extern Function1 (int x, int y);

This is enough to call Function1 from example.dll.

To call functions with structures as parameters, you need to make C# structure definitions. "Using Win32 and Other Libraries" article has such samples, for example, GetSystemPowerStatus which requires pointer to SYSTEM_POWER_STATUS structure.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
CoreSupportAuthor Commented:
Hi there AlexFM
Thanks for the link. We ve had a go at interoperabity. It looks like we would need to define several structs in c# to even use the most basic functions. :(
We have got char arrays working and basic types but lots of the C structs are using the HANDLE type

e.g.
typedef struct rs232Com
{
     HANDLE      hCom232;
     char      szComPort[COMPORT_LEN];
     int            baudrate;
} rs232Com;


We cant find any info on mapping this type.
Do you know off hand where we could find out a list of how C types map to C# ones?
Thanks for your help pointing us in the right direction AlexFM
Dave
Thanks
Dave



0
 
AlexFMCommented:
rs232Com structure can be declared like TIME_ZONE_INFORMATION structure from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp09192002.asp (see Structures with Embedded Char Arrays).
Since szComPort is char array and strings from TIME_ZONE_INFORMATION are WCHAR, you need to use CharSet = CharSet.Ansi instead of CharSet = CharSet.Unicode from the sample.
If you have problems with specific function or structure, ask question in C# area, you have better chance to get answer there.
0
 
AlexFMCommented:
BTW, HANDLE is translated to C# as IntPtr.
0
 
CoreSupportAuthor Commented:
Hi AlexFM,
Thanks a mill! we got some of the key functions working!
We had used the CharSet.Unicode setting and also tried using an int instead of IntPtr and we were scratching our heads a bit. Then we changed them and it worked. We should be able to use that as a template for the rest of the functions.
Great work AlexFM,Thanks
All the best
Dave
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now