473,395 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

C header to c#

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

Nov 17 '05 #1
2 5514
Fredrik,

See inline:
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?
You could declare this as internal, then only classes inside your
assembly can use it, but it won't be visible outside of the assembly.

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#?
For your application, you are going to have to do this. Publically, you
would want to expose your structure like this:

public struct session
{
public int id;
public string name;
}

Then, internally, you would use another structure which is passed to the
interop layer, like so:

[StructLayout(LayoutKind.Sequential)]
internal struct InternalSession
{
public int id;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=wrapperclass.NAME_LEN)]
public byte[] name;
}

Then your wrappers would take a type of session, and you would convert
the string into an array of bytes using the encoding classes in the
System.Text namespace. You could make this a single structure, and use a
custom marshaler as well (which is more elegant, IMO).

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?
You can only use the pointer if you are using unsafe code, and I don't
think you want to force your clients to do that. What you should do is use
the ref keyword, like so:

[DllImport("thelib.dll")]
public static extern int login(ref InternalSession sess, int orgnr, byte[]
name);

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?).
You will have to have a wrapper for this that will convert to the proper
encoding (through the System.Text namespace).

Lots of questions, so I send out a lot of thanks in advance :).

regards
//Fredrik Olsson


You might also want to revise the names on the public types. I suggest
you take a look at the guidelines for public naming conventions.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
Nov 17 '05 #2
<snip>

You might also want to revise the names on the public types. I suggest
you take a look at the guidelines for public naming conventions.

Hope this helps.


It helped very much, I already got all constants, structs and function
calles declared. At elast the compiler does not complain :). And I have been
able to login and get the basic user information fromt he server through c#.
So it is a major step forward, and a big thank you for pushing me in the
right direction!

regards
//Fredrik Olsson
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: John | last post by:
Hi. I am having a few header problems at the moment with a login page. I don't have my headers at the top of the page where I've learned I need to have them. However, I also know I'm supposed...
11
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do...
60
by: Derrick Coetzee | last post by:
It seems like, in every C source file I've ever seen, there has been a very definite include order, as follows: - include system headers - include application headers - include the header...
18
by: John Smith | last post by:
Hi all What does the group think of the practise of including one header file from inside another? I have some legacy code where this has been done, and it creates a dependency on a module...
6
by: Jason Collins | last post by:
There seems to be an inconsistency (bug?) in the way the Set-Cookie header is handled by the WebHeaderCollection. That is, the values of Set-Cookie, when an Expires is specified, contain the ","...
0
by: Dean Hallman | last post by:
Hello, I am developing a BHO that should add a custom HTTP header on a specific domain only. Don't want the header globally, otherwise I could just add a registry key. So, on...
4
by: Joseph Geretz | last post by:
We use a Soap Header to pass a token class (m_Token) back and forth with authenticated session information. Given the following implementation for our Logout method, I vastly prefer to simply code...
1
by: Shalako | last post by:
I check my error log and see these entries: malformed header from script. Bad header= Missing gauge reports are ind: padata.pl /perl/pema/padata.pl did not send an HTTP header malformed...
5
by: =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post by:
I have a sub in vb.net that adds extra headers to a gridview and it works very well. however, i tried to translate it to c# and i'm getting the header inserting itself over the first datarows and...
30
by: xiao | last post by:
HI~ EVERY ONE~ I have a small program here, when I tried to compile it , it always reminds that arrary.c: In function `main': arrary.c:39: error: `header' undeclared (first use in this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.