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 said:
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;
}
rights. Use of included script samples are subject to the terms specified at