DllImport: pointer to an array of far pointers to null-terminated character

P

Peter Gloor

Hello NG,

I have a mirror.dll written in C I would like to access from a C# console
application. However, I dont't know how to to it.

The dll is fairly well documented and I have access to the header files.
According to the documentation and the header files the function I need is
declared as follows:

extern "C" {
unsigned short far pascal InitMirror( int argc, char far * far *argv);
}

argc: # command line arguments the program was invoked with.
argv: pointer to an array of far pointers to null-terminated character
strings that contain the arguments the program was invoked with.
Returns zero if successfull, or a non-zero value if unable to initialize.

Now, I'm doing someting wrong, so my code always returns a non-zero value.

using System;
using System.Runtime.InteropServices;
namespace MirrorDemo
{
class Demo {
[DllImport("mirror.dll", EntryPoint="InitMirror",
SetLastError=false, CharSet=CharSet.Ansi, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern int InitMirror(int argc, String [] argv);

[STAThread]
static void Main(string[] args) {
int error;
error = InitMirror(args.Length, args);
if (error != 0) {
Console.WriteLine("Error: {0}", error);
}
}
}
}

This doesn't work as I already expected. However I dont't know how to do it.
How should the code look like?

Any hints are welcome!

Thanks in advance.

Peter Gloor
 
T

Ted Miller

Well, independently of any other problems you might be having, your C++
routine returns a 16 bit unsigned value, but your p/invoke declaration is
for a 32 bit signed value. So you are likely getting garbage in the upper 16
bits on the managed side, which could account for your troubles.

Also, you might want to double-check the default marshalling for arrays of
strings to make sure that string[] marshals to char** and not to a
safearray, or an array of bstr's, etc. (I think the default behavior is
probably to marshal as an in param of char** as you are assuming, just
wanted to point that out.)
 

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