Call C functions in C#

C

Christopher Sin

[repost, nobody have seen this message ?]

Hi,

First, if I am on a wrong group, please tell me,
therefore, i think this one is correct.

Second, please ignore my poor English speaking.

I have to write a c# wrapper to a C library.
This is OK, but I have difficulties with some functions
that deals with double indirections.

The C function is
ectern "C" {
UNIRESULT_S __declspec(dllexport) * __stdcall
uni_open_session (const char * args, UNISESSION_S * *
psession);
}

UNIRESULT_S and UNISESSION_S just are structures (but with
incomplete definition in headers).

This function allocate ressources and retruned this by the
psession pointer.
The returned value is a pointer on a structure.

I have tried this :

[DllImport("clirts32.dll", CharSet=CharSet.Ansi)]
public static extern IntPtr uni_open_session(String args,
ref IntPtr psession) ;

This works in some cases, but provoque an
NullPointerException in others.
(the case where it is works is console application
launcher, where with a web application aspnet, it is not).

The C function is part of a dll, given by a vendor.
He gave me also with .h include files, an vb6 example file.

This last file contains this :

Declare Function uni_open_session Lib "clirts32.dll"
(ByVal args As String, psession As Long) As Long

What is the equivalent in C# ?

How to use it ?

In my C# code, i use C function like this :

string args = "servername etc." ;
IntPtr m_session = new IntPtr(0) ;
IntPtr hresult = Clirts32.uni_open_session(args, ref
m_session) ;

The VB6 example code is

Dim session As Long ' Session-Handle
Dim result As Long
result = uni_open_session(arg_str, session)

Is someone have any idea ?

Is my code correct, and the probleme came from aspnet
references dlls ?
Or is my code incorrect ?

Thank you.
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi,

I believe you might get more help in the
microsoft.public.dotnet.framework.interop newsgroup.
 
C

Christopher sin

Ok, thank you for your reply.
I will post there.
-----Original Message-----
Hi,

I believe you might get more help in the
microsoft.public.dotnet.framework.interop newsgroup.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

[repost, nobody have seen this message ?]

Hi,

First, if I am on a wrong group, please tell me,
therefore, i think this one is correct.

Second, please ignore my poor English speaking.

I have to write a c# wrapper to a C library.
This is OK, but I have difficulties with some functions
that deals with double indirections.

The C function is
ectern "C" {
UNIRESULT_S __declspec(dllexport) * __stdcall
uni_open_session (const char * args, UNISESSION_S * *
psession);
}

UNIRESULT_S and UNISESSION_S just are structures (but with
incomplete definition in headers).

This function allocate ressources and retruned this by the
psession pointer.
The returned value is a pointer on a structure.

I have tried this :

[DllImport("clirts32.dll", CharSet=CharSet.Ansi)]
public static extern IntPtr uni_open_session(String args,
ref IntPtr psession) ;

This works in some cases, but provoque an
NullPointerException in others.
(the case where it is works is console application
launcher, where with a web application aspnet, it is not).

The C function is part of a dll, given by a vendor.
He gave me also with .h include files, an vb6 example file.

This last file contains this :

Declare Function uni_open_session Lib "clirts32.dll"
(ByVal args As String, psession As Long) As Long

What is the equivalent in C# ?

How to use it ?

In my C# code, i use C function like this :

string args = "servername etc." ;
IntPtr m_session = new IntPtr(0) ;
IntPtr hresult = Clirts32.uni_open_session(args, ref
m_session) ;

The VB6 example code is

Dim session As Long ' Session-Handle
Dim result As Long
result = uni_open_session(arg_str, session)

Is someone have any idea ?

Is my code correct, and the probleme came from aspnet
references dlls ?
Or is my code incorrect ?

Thank you.

.
 
N

Nicholas Paldino [.NET/C# MVP]

Christopher,

From the VB code, it actually looks like you want this:

[DllImport("clirts32.dll", CharSet=CharSet.Ansi)]
public static extern IntPtr uni_open_session(String args, IntPtr psession);

And then you pass in the psession variable as you would normally. You
then call it like this:

IntPtr result = uni_open_session(arg_str, IntPtr.Zero);

Hope this helps.
 

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