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(La youtKind.Explic it)]
public struct session {
[FieldOffset(0)] public int id;
[FieldOffset(4)] public byte[] name = new byte[wrapperclass.NA ME_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("thel ib.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("thel ib.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 2 5532
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(La youtKind.Explic it)] public struct session { [FieldOffset(0)] public int id; [FieldOffset(4)] public byte[] name = new byte[wrapperclass.NA ME_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(La youtKind.Sequen tial)]
internal struct InternalSession
{
public int id;
[MarshalAs(Unman agedType.ByValA rray, SizeConst=wrapp erclass.NAME_LE N)]
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("thel ib.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("thel ib.dll")]
public static extern int login(ref InternalSession sess, int orgnr, byte[]
name); Trouble #4: The call, third argument: [DllImport("thel ib.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.co m
<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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 to have the
session_start(); at the top of the page as well. So when you have two
things that need to be the first which do you put first? And if I put
one before the other will that cause problems?
Below is a copy of the php from my page. It...
|
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 understand the
objective. This example may represent an interface in need of a bit of
refactoring, but it goes to demonstrate the basic idea as I understand it.
http://developer.kde.org/documentation/library/cvs-api/kdevelop/html/ast_8h-source.html...
|
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 associated with this source file
For example, in a file hello.c:
#include <stdio.h>
|
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 (collection of files) which are not required, except
for one header file's contents.
I'd say 'No, header files should be included in the C source, not in another
|
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 "," character. This seems to be
incorrectly parsed during GetValues().
A simple example shows it best (there are 2 .aspx pages and an output):
AddHeaders.aspx:
<%@ Page language="c#" %>
| |
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 BEFORENAVIGATE2, I am canceling the current navigation and
renavigating with the new custom header using Navigate2. This usually
works fine, but in some cases (particularly when selecting a link that
starts a new window), the Navigate2 call fails.
|
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 m_Token = null in order to
destroy the session token when the user logs out. However, I'm finding that
setting class instance to null results in no header being sent back to the
client, with the result that the client actually remains with an...
|
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 header from script. Bad header= : padata.pl
/perl/pema/padata.pl did not send an HTTP header
malformed header from script. Bad header= Missing gauge reports are ind: padata.pl
/perl/pema/padata.pl did not send an HTTP header
malformed...
|
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 inserting blank rows at the bottom.
i can post the code if necessary for the C# but it basicly mirrors the vb.net
--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
|
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 function)
arrary.c:39: error: (Each undeclared identifier is reported only once
arrary.c:39: error: for each function it appears in.)
Why is that ? I think I have declared it . :(
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |