Probem Converting C/C++ Windows code to Visual C#

The code Im trying to convert is shown below. It returns the pointer to a COM port in the variable "CommPort". The function "InitPort" is in a DLL and its signature is also shown below.

int vReturn = InitPort(CommPort);

 __declspec(dllexport) int  InitPort(HANDLE &mhCommunicationPort);

Now this code works fine in the windows C/C++ application. "commPort" returns a pointer to a COM port as expected.

But I need to get this working in a visual C# application (dont ask why!).
Ive been told the equivalent of HANDLE in C# is IntPtr so Ive been trying this in my C# application.

static extern int InitPort(IntPtr h);

IntPtr CommPort = new IntPtr(-1);
int vReturn = InitPort(CommPort);

but Im getting an exception somewhere in the bowels of the "InitPort" function.

Has anyone any idea why this may be failing?

I dont believe I should have to delve into the actual code of "InitPort" (not at this stage anyway) since I should just be trying to find the right datatype to match up the C# and C/C++ Windows code.
Who is Participating?
Since the C code specifies that the handle's passed by reference, you could try changing the C# extern definition and the subsequent call:

static extern int InitPort(ref IntPtr h);

IntPtr CommPort = new IntPtr(-1);
int vReturn = InitPort(ref CommPort);

What's the exception you're getting? Can't actually see the above change fixing it but it might be worth a go
rangers99Author Commented:

Many, many thanks. You've fixed it buddy. Cheers
You may think of a mixed assembly (dll) where managed C++ and unmanaged C++ were linked together.

Then the C# could call the managed functions of that assembly while the managed functions in the C++ mixed assembly could call the unmanaged functions without any problems.


Note, the mixed code dll is of project type CLR - Class Library. To use it in the C# add the dll file to the references.

See more at

// C++ (all in one IJW assembly)
#include <iostream>
// the code below is only for compile
// use the C++ headers where InitPort function was supplied
typedef void *HANDLE;
int InitPort(HANDLE& cp)
   cp = ((HANDLE)(0));
   return 0;
namespace UnmanagedCode
   HANDLE InitPort()
       int vReturn = ::InitPort(commPort);
       if (vReturn == 0 && commPort != INVALID_HANDLE_VALUE)
          return commPort;
       return NULL; 
using System::IntPtr;
namespace ManagedCode
   public ref class InitPortWrapper
         static IntPtr InitPort()
            HANDLE h = UnmanagedCode::InitPort();
            IntPtr ip(h);
            return ip;
// C# code (seperate assembly)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ManagedCode;
namespace testinitport
    class Program
        static void Main(string[] args)
            IntPtr ip = InitPortWrapper.InitPort();

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.