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.

HANDLE CommPort = INVALID_HANDLE_VALUE;
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.

[DllImport(@"MyFunctionDLL.dll")]
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.
rangers99Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PablissimoCommented:
Since the C code specifies that the handle's passed by reference, you could try changing the C# extern definition and the subsequent call:

[DllImport(@"MyFunctionDLL.dll")]
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rangers99Author Commented:
Pablissimo

Many, many thanks. You've fixed it buddy. Cheers
0
itsmeandnobodyelseCommented:
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.

See

http://msdn.microsoft.com/en-us/library/x0w2664k.aspx


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  http://www.ondotnet.com/pub/a/dotnet/2004/03/29/mcpp_part3.html?page=1

 
// 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;
#define INVALID_HANDLE_VALUE		((HANDLE)(-1))
 
int InitPort(HANDLE& cp)
{
   cp = ((HANDLE)(0));
   return 0;
}
 
namespace UnmanagedCode
{
   HANDLE InitPort()
   {
       HANDLE commPort = INVALID_HANDLE_VALUE;
       int vReturn = ::InitPort(commPort);
       if (vReturn == 0 && commPort != INVALID_HANDLE_VALUE)
          return commPort;
       return NULL; 
   }  
 
}
 
using System::IntPtr;
 
 
namespace ManagedCode
{
   public ref class InitPortWrapper
   {
      public:
         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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.