Problem when importing a C++ function on Windows CE 4.2

J

jman0

I have developed a C++ DLL with Embedded Visual C++ 4.0. Inside it
there's a function which in the past returned a bool value, and worked
perfectly when called from a C# project.

Now, I need that function to return a DWORD value. In C++, the
function header is as follows:

extern "C" LIB_API DWORD StartRASConnection(unsigned short *Name,
unsigned short *UserName, unsigned short *Password)

where LIB_API is declared as

#ifdef MIPLIB_EXPORTS
#define LIB_API __declspec(dllexport)
#else
#define LIB_API __declspec(dllimport)
#endif

In C#, function is imported as

[DllImport("miplib.dll")]
public static extern long StartRASConnection(string nombreConn, string
usuario, string pass);

The problem is that when I call StartRASConnection, an exception is
raised. The exception text is just "NotSupportedException". I think
the problem must be on the C# side, since there's a try...catch block
on the C++ code, which catches all exceptions and then returns the
number 25000 in case one is raised. When the same try...catch block
just returned 0 in the past (when the function returned bool) there
was no "NotSupportedException" issue.

When the C++ function returned a bool, everything worked fine, which
is driving me crazy.

Where's the problem with my code?

Thanks in advance.
 
B

Ben Voigt [C++ MVP]

jman0 said:
I have developed a C++ DLL with Embedded Visual C++ 4.0. Inside it
there's a function which in the past returned a bool value, and worked
perfectly when called from a C# project.

Now, I need that function to return a DWORD value. In C++, the
function header is as follows:

extern "C" LIB_API DWORD StartRASConnection(unsigned short *Name,
unsigned short *UserName, unsigned short *Password)

where LIB_API is declared as

#ifdef MIPLIB_EXPORTS
#define LIB_API __declspec(dllexport)
#else
#define LIB_API __declspec(dllimport)
#endif

In C#, function is imported as

[DllImport("miplib.dll")]
public static extern long StartRASConnection(string nombreConn, string
usuario, string pass);

The problem is that when I call StartRASConnection, an exception is
raised. The exception text is just "NotSupportedException". I think
the problem must be on the C# side, since there's a try...catch block
on the C++ code, which catches all exceptions and then returns the
number 25000 in case one is raised. When the same try...catch block
just returned 0 in the past (when the function returned bool) there
was no "NotSupportedException" issue.

When the C++ function returned a bool, everything worked fine, which
is driving me crazy.

Where's the problem with my code?

C# long isn't the same size as C++ DWORD. Use System.UInt32, which is the
correct equivalent to DWORD.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top