NEWBIE: Accessing DLL with P/Invoke

M

michael

Hello,

My apologies for my overtly NEWBIE question. I am a long time C
programmer on various UNIX platforms. I have decided to try my hand at
Microsoft and thought I would try to learn C#.

My project involves accessing an API whish was written in C and
compiled using Visual Studio 6 as an ordinary (old style) DLL. I have
been provided the DLL and corresponding header files.

My project is being developed under Visual C# Express 2005 Beta 2 and I
am relying on the P/Invoke funcationality.

I believe I have correctly created all of the definitions following the
DllImport() rules (including the "unsafe" modifier, etc).

I am able to make function calls to the DLL (I think!) but the function
call is returning an "unknown network error." (I know this because I
have a list of error codes in one of the header files.)

This got me thinking, what happens when that DLL makes another external
function call? In this case, the DLL is using the IP stack of the OS
to communicate a proprietary protocol with an off-site box (which I
cannot access directly).

If I compile this as a ".NET" application, then does that not mean that
all functions (including IP stack) must go through the .NET VM (forgot
what it's called)? Or should access to the IP stack from "old style"
DLLs work anyway?

Any help here is appreciated. Please email me directly or cc: me with
responses.

Thanks!

Michael
 
N

Nicholas Paldino [.NET/C# MVP]

Michael,

Once you call into an unmanaged piece of code, the runtime can't
interfere in that call. The calls are made as if any other piece of code
was accessing them.

What you are seeing is most likely a setup issue, or a legitimate
network issue. It's not the runtime interfering.

It is possible that your P/Invoke declarations are incorrect, so that
the parameters that are being passed in are not correct, causing the
function to run in an unexpected way. You can post the declarations (in
your C# program, and the header file), so that we can double-check them.

Test this out by calling the dll from a C program. Does it work still?

Hope this helps.
 
M

Michael Ferioli

Thanks! Here is the code:

From the C header files:

typedef unsigned __int32 MXSUInt32;
typedef __int64 MXSLong;
typedef MXSLong MXSStoreRef;

EXPORT MXSInt32 DECL mxsOpenStore (const char* clusterId,
const char* address,const char* serverKey,
MXSUInt32 serverKeyLen, MXSStoreRef* storePtr);

From my C# code:

namespace MyNameSpace
{
class MxsApi
{
[DllImport("mxsapidll32.dll" ) ]
public static extern unsafe Int32 mxsOpenStore(void* clusterId,
void* address, void* serverKey, UInt32 serverKeyLen,
Int64* storePtr);
}
class Listener
{
static unsafe void Main(string[] args)
{
Int64 storePtr = 0;
Int32 Result=34;
char[] clusterId = {'x','v','e','d','6','3','a','1','-','5',
'b','l','8',',','f','5','0','f','\0'};
char[] addresses = {'2','1','2','.','2','0','.',
'1','2','4','.','1','1','7','\0'};
char[] serverKey = { '\0' };

fixed (void* clusterPointer = clusterId,
addressesPointer = addresses,
serverKeyPointer = serverKey)
{
System.Console.WriteLine("Result={0} cluster={1}",
Result, clusterId);
Result = MxsApi.mxsOpenStore(clusterPointer,
addressesPointer, serverKeyPointer, 0, &storePtr);
System.Console.WriteLine("Result={0}", Result);
}

}
}
}
 

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