G
Guest
I have dynamic library in a DLL. With a standard c header file to it. Writing
a simple COM-wrapper in Visual Basic 6 was quite simple, and proves effective
in providing my library's functionality to VB and VBScript.
Now I want to let the .NET developers have just as much of a breeze, but it
has proven not to be quite as easy. I want to write the .net class library in
c# and I can not change the original API in anyway as it is a deployed
application.
Lets say I have this defined in a C header:
// Some constants
#define NAME_LEN 16
// A very simple struct
typedef struct {
int id;
char name[NAME_LEN];
} session;
// And a function taking various arguments.
int login(session *sess, int orgnr, char *name);
The approach I am heading for is to have the C API in a private wrapper
class in the name-space, and then write new wrapper classes in c# with calls
to static methods in this class. I think it would hide the inner workings
good from the end user and keep it simple for me (It worked for the Java,
C++, Objective-C, Perl and COM wrappers already done at least).
Trouble #1:
#define NAME_LEN 16
This would translate to:
private const int NAME_LEN = 16;
If it could be done in the name space, now I can not so I have to go with
public in the private wrapper class or make it part of a private enum in the
name space. What method is preferred?
Trouble #2:
The struct, something like this:
[StructLayout(LayoutKind.Explicit)]
public struct session {
[FieldOffset(0)] public int id;
[FieldOffset(4)] public byte[] name = new byte[wrapperclass.NAME_LEN];
}
I go for byte instead of char as char is Unicode in c# and I need basic
LATIN1 encoding. But I somehow doubt this will do anyway, as my bet is that
byte[] and new byte[...] does quite allot of magic behind the scene that is
as far from what C does as one can come . So question is; how do I define
the struct to be compatible in c#?
Trouble #3:
The call, first argument:
[DllImport("thelib.dll")]
public static extern int login(session* sess, int orgnr, byte *name);
Is syntactically correct, but it is the best way? I see the keyword ref
being mentioned allot, and I think it would work just as well in this case.
But I want to pass around the struct allot (Being a session struct it will be
used by all other functions as one can guess) so using a pointer seams more
natural?
Trouble #4:
The call, third argument:
[DllImport("thelib.dll")]
public static extern int login(session* sess, int orgnr, byte *name);
Me no like it at all, does not seem to me like the right way to do it. The
string class have a ToCharArray method, but that on is Unicode as well. And
as I said, Unicode is no good to me, only LATIN1. So how do I pass simply
c-strings? (And retrieve them?).
Lots of questions, so I send out a lot of thanks in advance .
regards
//Fredrik Olsson
a simple COM-wrapper in Visual Basic 6 was quite simple, and proves effective
in providing my library's functionality to VB and VBScript.
Now I want to let the .NET developers have just as much of a breeze, but it
has proven not to be quite as easy. I want to write the .net class library in
c# and I can not change the original API in anyway as it is a deployed
application.
Lets say I have this defined in a C header:
// Some constants
#define NAME_LEN 16
// A very simple struct
typedef struct {
int id;
char name[NAME_LEN];
} session;
// And a function taking various arguments.
int login(session *sess, int orgnr, char *name);
The approach I am heading for is to have the C API in a private wrapper
class in the name-space, and then write new wrapper classes in c# with calls
to static methods in this class. I think it would hide the inner workings
good from the end user and keep it simple for me (It worked for the Java,
C++, Objective-C, Perl and COM wrappers already done at least).
Trouble #1:
#define NAME_LEN 16
This would translate to:
private const int NAME_LEN = 16;
If it could be done in the name space, now I can not so I have to go with
public in the private wrapper class or make it part of a private enum in the
name space. What method is preferred?
Trouble #2:
The struct, something like this:
[StructLayout(LayoutKind.Explicit)]
public struct session {
[FieldOffset(0)] public int id;
[FieldOffset(4)] public byte[] name = new byte[wrapperclass.NAME_LEN];
}
I go for byte instead of char as char is Unicode in c# and I need basic
LATIN1 encoding. But I somehow doubt this will do anyway, as my bet is that
byte[] and new byte[...] does quite allot of magic behind the scene that is
as far from what C does as one can come . So question is; how do I define
the struct to be compatible in c#?
Trouble #3:
The call, first argument:
[DllImport("thelib.dll")]
public static extern int login(session* sess, int orgnr, byte *name);
Is syntactically correct, but it is the best way? I see the keyword ref
being mentioned allot, and I think it would work just as well in this case.
But I want to pass around the struct allot (Being a session struct it will be
used by all other functions as one can guess) so using a pointer seams more
natural?
Trouble #4:
The call, third argument:
[DllImport("thelib.dll")]
public static extern int login(session* sess, int orgnr, byte *name);
Me no like it at all, does not seem to me like the right way to do it. The
string class have a ToCharArray method, but that on is Unicode as well. And
as I said, Unicode is no good to me, only LATIN1. So how do I pass simply
c-strings? (And retrieve them?).
Lots of questions, so I send out a lot of thanks in advance .
regards
//Fredrik Olsson