Solved

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

Posted on 2009-07-07
3
275 Views
Last Modified: 2012-05-07
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.
0
Comment
Question by:rangers99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Accepted Solution

by:
Pablissimo earned 250 total points
ID: 24793267
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
 

Author Closing Comment

by:rangers99
ID: 31600540
Pablissimo

Many, many thanks. You've fixed it buddy. Cheers
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24794611
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question