473,408 Members | 2,813 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,408 software developers and data experts.

Calling C Dll, function prototype

Hi,
I must call a C dll that export this function:

void __stdcall leggimet(const char* fname, unsigned char* buf, unsigned long
bufSize, char* extraInfo, long* resp)

There is anyone that suggest me DLLImport C# sintax?
I try:

[DllImport("c:\\temp\\leggimet.dll",
CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall,
SetLastError=true)]
public void leggimet(string fName, StringBuilder buf, UInt32 bufSize,
StringBuilder extrainfo, ref Int32 resp);

but the DLL cannot open the file... It is an UNICODE problem?
Thank you
G
Nov 16 '05 #1
6 2020
Hi,
Thank you.
I have tried your suggestion writing this code:

============== CUT HERE ==================
[DllImport("c:\\temp\\leggimet.dll",
CharSet=CharSet.Ansi,
CallingConvention=CallingConvention.StdCall,
SetLastError=true)]
public static extern void leggimet(string fName,
System.String buf,
System.UInt32 bufSize,
System.String extrainfo,
System.IntPtr resp);

private unsafe void button1_Click(object sender, System.EventArgs e)
{
System.UInt32 Buf_Size=1250*625;
string fName="c:\\temp\\BW__0500.HRM";
System.String buf=new System.String('\0',(int)(Buf_Size));
System.String extraInfo = new System.String('\0',256);
System.IntPtr pResp;
System.Int32 resp=20;

pResp=new System.IntPtr(&resp);

leggimet(fName,buf,Buf_Size,extraInfo,pResp);
}

============== CUT HERE ==================

but when the debug execute the dll call (leggimet) I obtain an exception of
type: System.NullReferenceException
Can you help me?
I would remeber you that the var buf, extraInfo and resp must be modified by
the DLL function...
Thank you very much.
G

"Jackson Davis [MSFT]" <an*******@discussions.microsoft.com> ha scritto nel
messaggio news:04**********************************@microsof t.com...
Gaetano,

I got this to work using the following signature:
[DllImport("c:\\temp\\leggimet.dll",
CharSet=CharSet.Ansi,
CallingConvention=CallingConvention.StdCall,
SetLastError=true)]
public void leggimet(string fName,
System.String buf,
System.UInt32 bufSize,
System.String extrainfo,
System.IntPtr resp);

I did have to specify Ansi or the strings were not marshalled correctly.

Jackson Davis [MSFT]
--
This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm

Nov 16 '05 #2
Alright, now I see what you are doing. It wasn't obvious which parameters were in, out and in/out. Pass StringBuilders for string that need to be marshalled as in/out. You need to initialize them to the maximum size before passing them in. Also, you should mark the IntPtr as out. In order to initialize the long*, I had to use the address keyword in an unsafe context.
---- Example driver (must be compiled with /unsafe ----
using System;
using System.Text;
using System.Runtime.InteropServices;

public class blah
{
[DllImport("C:\\temp\\test\\test.dll",
CharSet=CharSet.Ansi,
CallingConvention=CallingConvention.StdCall,
SetLastError=true)]
static extern void leggimet([In] String fName,
[Out] StringBuilder buf,
[In] UInt32 bufSize,
[Out]StringBuilder extrainfo,
[Out]IntPtr resp);

public static void Main()
{
uint bufSize = 10;
StringBuilder buf = new StringBuilder((int)bufSize);
StringBuilder extraInfo = new StringBuilder(10); // Whatever is the max size of extrainfo.
int resp;

unsafe {
leggimet("blah", buf, bufSize, extraInfo, new IntPtr(&resp));
}

System.Console.WriteLine(buf);
System.Console.WriteLine(extraInfo);
System.Console.WriteLine(resp);
}
}

-- Example dll source ----

#include <stdio.h>

void __stdcall leggimet(const char* fname,
unsigned char* buf,
unsigned long bufSizem,
char* extraInfo,
long* resp)
{
printf("%s\n", fname);
strcpy(buf, "Blah");
strcpy(extraInfo, "Blah2");
*resp = 10;
}
----------
Hope that helps
Jackson Davis [MSFT]
--
This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
Nov 16 '05 #3
Hi,

"Gaetano D'Aquila" <gd************@tiscali.it> wrote in message
news:c6************@ID-68966.news.uni-berlin.de...
Hi,
I must call a C dll that export this function:

void __stdcall leggimet(const char* fname, unsigned char* buf, unsigned long bufSize, char* extraInfo, long* resp)

There is anyone that suggest me DLLImport C# sintax?
I try:

[DllImport("c:\\temp\\leggimet.dll",
CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall,
SetLastError=true)]
public void leggimet(string fName, StringBuilder buf, UInt32 bufSize,
StringBuilder extrainfo, ref Int32 resp);
Try it like this:

public static extern void leggimet(string fName, [In,Out] byte[] buf, uint
bufSize, StringBuilder extraInfo, ref int resp);

void test()
{
uint size = 1250*625;
byte[] buffer = new buffer[size];
string fName = "c:\\temp\\BW__0500.HRM";
StringBuilder extraInfo = new StringBuilder(256);
uint resp = 0;
leggimet ( fName, buffer, size, extraInfo, ref resp );
}
If you really want to use a string as buffer, then you can use a
stringbuilder and set charset to ansi, but it's more common to map an
unsigned char array to a byte array.

HTH,
Greetings


but the DLL cannot open the file... It is an UNICODE problem?
Thank you
G

Nov 16 '05 #4
Hi,
I have tryed your code but I again obtain an Exception.
I'm working on this problem from about 1 week without result.
I have written the same procedure in C++ and it works correctly.
My C++ code is:
============== CUT ====================
typedef void (__stdcall *MYPROC)(char*, unsigned char*, unsigned long,
char*, long*);

HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;

char fName[256]="c:\\temp\\BW__0500.HRM\0";
long resp=20;
unsigned char buf[1250*625];
unsigned long bufSize=625*1250;
char extraInfo[256];

hinstLib = LoadLibrary("c:\\temp\\leggimet.dll"); // Get a handle to the
DLL module.
if (hinstLib != NULL) // If the handle is valid, try to get the function
address.
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "leggimet");
if (NULL != ProcAdd) // If the function address is valid, call the
function.
{
fRunTimeLinkSuccess = TRUE;
(ProcAdd) (fName,buf,bufSize,extraInfo,&resp);
}
}
============== CUT ====================
If you want, I can send you my test DLL (wich I haven't the source code) and
my C# project.
I cant see nobody solution.
Can you help me?
Thank you very much.
"Jackson Davis [MSFT]" <an*******@discussions.microsoft.com> ha scritto nel
messaggio news:F7**********************************@microsof t.com...
Alright, now I see what you are doing. It wasn't obvious which parameters were in, out and in/out. Pass StringBuilders for string that need to be
marshalled as in/out. You need to initialize them to the maximum size before
passing them in. Also, you should mark the IntPtr as out. In order to
initialize the long*, I had to use the address keyword in an unsafe context. ---- Example driver (must be compiled with /unsafe ----
using System;
using System.Text;
using System.Runtime.InteropServices;

public class blah
{
[DllImport("C:\\temp\\test\\test.dll",
CharSet=CharSet.Ansi,
CallingConvention=CallingConvention.StdCall,
SetLastError=true)]
static extern void leggimet([In] String fName,
[Out] StringBuilder buf,
[In] UInt32 bufSize,
[Out]StringBuilder extrainfo,
[Out]IntPtr resp);

public static void Main()
{
uint bufSize = 10;
StringBuilder buf = new StringBuilder((int)bufSize);
StringBuilder extraInfo = new StringBuilder(10); // Whatever is the max size of extrainfo. int resp;

unsafe {
leggimet("blah", buf, bufSize, extraInfo, new IntPtr(&resp));
}

System.Console.WriteLine(buf);
System.Console.WriteLine(extraInfo);
System.Console.WriteLine(resp);
}
}

-- Example dll source ----

#include <stdio.h>

void __stdcall leggimet(const char* fname,
unsigned char* buf,
unsigned long bufSizem,
char* extraInfo,
long* resp)
{
printf("%s\n", fname);
strcpy(buf, "Blah");
strcpy(extraInfo, "Blah2");
*resp = 10;
}
----------
Hope that helps
Jackson Davis [MSFT]
--
This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm

Nov 16 '05 #5
Thank you for your response.
I have tried your solution but does not work.
When I execute leggimet(...) I obtain an exception:
System.NullReferenceException
Please see my last message in the newsgroup for more information.
Have another idea?
Thank you.

"BMermuys" <so*****@someone.com> ha scritto nel messaggio
news:OZ*************@TK2MSFTNGP09.phx.gbl...
Hi,

"Gaetano D'Aquila" <gd************@tiscali.it> wrote in message
news:c6************@ID-68966.news.uni-berlin.de...
Hi,
I must call a C dll that export this function:

void __stdcall leggimet(const char* fname, unsigned char* buf, unsigned

long
bufSize, char* extraInfo, long* resp)

There is anyone that suggest me DLLImport C# sintax?
I try:

[DllImport("c:\\temp\\leggimet.dll",
CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall,
SetLastError=true)]
public void leggimet(string fName, StringBuilder buf, UInt32 bufSize,
StringBuilder extrainfo, ref Int32 resp);


Try it like this:

public static extern void leggimet(string fName, [In,Out] byte[] buf, uint
bufSize, StringBuilder extraInfo, ref int resp);

void test()
{
uint size = 1250*625;
byte[] buffer = new buffer[size];
string fName = "c:\\temp\\BW__0500.HRM";
StringBuilder extraInfo = new StringBuilder(256);
uint resp = 0;
leggimet ( fName, buffer, size, extraInfo, ref resp );
}
If you really want to use a string as buffer, then you can use a
stringbuilder and set charset to ansi, but it's more common to map an
unsigned char array to a byte array.

HTH,
Greetings


but the DLL cannot open the file... It is an UNICODE problem?
Thank you
G


Nov 16 '05 #6
Hi,

"Gaetano D'Aquila" <gd************@tiscali.it> wrote in message
news:c6************@ID-68966.news.uni-berlin.de...
Thank you for your response.
I have tried your solution but does not work.
When I execute leggimet(...) I obtain an exception:
System.NullReferenceException
I've created a dll function with the same prototype and used the c# code I
posted and it works. The prototype isn't really that special. So I have no
idea why you get this error...

There was a typo in my reply but I guess you saw it : byte[] buffer = new
buffer[size] should offcourse be byte[] buffer = new byte[size].
Please see my last message in the newsgroup for more information.
Have another idea?
If you want mail relevant stuff to b-*******@pandora.be

Greetings
Thank you.

"BMermuys" <so*****@someone.com> ha scritto nel messaggio
news:OZ*************@TK2MSFTNGP09.phx.gbl...
Hi,

"Gaetano D'Aquila" <gd************@tiscali.it> wrote in message
news:c6************@ID-68966.news.uni-berlin.de...
Hi,
I must call a C dll that export this function:

void __stdcall leggimet(const char* fname, unsigned char* buf,
unsigned long
bufSize, char* extraInfo, long* resp)

There is anyone that suggest me DLLImport C# sintax?
I try:

[DllImport("c:\\temp\\leggimet.dll",
CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall,
SetLastError=true)]
public void leggimet(string fName, StringBuilder buf, UInt32 bufSize,
StringBuilder extrainfo, ref Int32 resp);


Try it like this:

public static extern void leggimet(string fName, [In,Out] byte[] buf, uint bufSize, StringBuilder extraInfo, ref int resp);

void test()
{
uint size = 1250*625;
byte[] buffer = new buffer[size];
string fName = "c:\\temp\\BW__0500.HRM";
StringBuilder extraInfo = new StringBuilder(256);
uint resp = 0;
leggimet ( fName, buffer, size, extraInfo, ref resp );
}
If you really want to use a string as buffer, then you can use a
stringbuilder and set charset to ansi, but it's more common to map an
unsigned char array to a byte array.

HTH,
Greetings


but the DLL cannot open the file... It is an UNICODE problem?
Thank you
G



Nov 16 '05 #7

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

Similar topics

5
by: Da Costa Gomez | last post by:
Hi, I was wondering whether someone could shed some light on the following. Using inheritance in Java one can override a function f() (or is it overload?) in the child and then do: public f() {...
21
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am...
12
by: Yuan Zhong | last post by:
Hi, Can someone please explain me what's going on during a call to a function. Specifically, I wanted to know what's going on in Stacks. Why is it ok to pass only 2 parameters or 5 parameters...
41
by: Telmo Costa | last post by:
Hi. I have the following code: -------------------------------------- function Tunnel() { //arguments(???); } function Sum() { var sum = 0; for (i=0; i<arguments.length; i++) sum +=...
12
by: torbs | last post by:
Hi I have a a function with several methods. For simplicity it looks a bit like this: super.prototype.aProperty="HELLO"; super.prototype.returnValue = function () { return 2;
5
by: Daz | last post by:
Hi everyone. My query is very straight forward (I think). What's the difference between someFunc.blah = function(){ ; } and
1
by: Memphis Steve | last post by:
Is it possible to combine multiple javascipts into one file and then call that file from a linked URL in the head section of an XHTML file? Here are the two scripts I want to use with the...
1
by: tuka | last post by:
Hi, I have 2 methods defined within a javascript objects that I would like to call in a third function within the same object. In the example below, func3 works well in FF but in IE7 it breaks....
7
by: Andrew Poulos | last post by:
Say I have this foo = function() { // blah }; bar = fuction(fnName) { /* If fnName equalled "foo" * How do I test that foo, as a function, * exists and then, if it exists, call it?
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: 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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
agi2029
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,...
0
isladogs
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...

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.