473,832 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PInvoke and Unmanaged DLL calls

Has anyone made use of PKWare's PKCDL.DLL for the Implode and Explode
functions?

I have been able to successfully define everything correctly and even
get the invoked function (Explode) to run properly. This means data is
being exploded properly, confirmed with CRC values and reviewing the
actual data.

However just before the unmanaged function exits, I get a null exception
error. I am using:

ReadHandler rh = new ReadHandler(thi s.ReadData);
WriteHandler wh = new WriteHandler(th is.WriteData);

IntPtr buffer = Marshal.AllocHG lobal(bufferSiz e)
try {
object data = this;

// Invoke DLL here...
PkCdl.Explode(r h, wh, buffer, ref data);

} finally {
Marshal.FreeHGl obal(buffer);
}

to allocate the needed buffer for the DLL. In the corresponding
ReadData/WriteData callback methods, I use the Marshal.Copy() methods to
move data in/out of managed memory.

Now somewhere along the way an the exception happens inside the
try..finally (shown above).

If this rings a bell with anyone, I will gladly supply complete sample
code and would be more than interested in hearing your thoughts.
Thanks,
Joe Martin

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
11 4657
Joe Martin wrote:
ReadHandler rh = new ReadHandler(thi s.ReadData);
WriteHandler wh = new WriteHandler(th is.WriteData);

IntPtr buffer = Marshal.AllocHG lobal(bufferSiz e)
try {
object data = this;

// Invoke DLL here...
PkCdl.Explode(r h, wh, buffer, ref data);

} finally {
Marshal.FreeHGl obal(buffer);
}


you have to pin "rh" and "wh", otherwise they are GCed.

....
} finally {
Marshal.FreeHGl obal(buffer);
}
GC.KeepAlive(rh );
GC.KeepAlive(wh );
bye
Rob
Nov 16 '05 #2
>you have to pin "rh" and "wh", otherwise they are GCed.

...
} finally {
Marshal.FreeHGl obal(buffer);
}
GC.KeepAlive(r h);
GC.KeepAlive(w h);

The runtime will keep the parameters alive for the duration of the
call. Manually keeping the delegates alive is only necessary when the
callbacks are asynchronous. And if they are, GC.KeepAlive wont help
you.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #3
Mattias Sjögren wrote:
you have to pin "rh" and "wh", otherwise they are GCed.

...
} finally {
Marshal.FreeHGl obal(buffer);
}
GC.KeepAlive( rh);
GC.KeepAlive( wh);


The runtime will keep the parameters alive for the duration of the
call. Manually keeping the delegates alive is only necessary when the
callbacks are asynchronous. And if they are, GC.KeepAlive wont help
you.


Ok. I see. I had a case where the delegates had to be setup
before their call:

DelegateType someDelegate = new ...;
UnmanagedFuncRe gisterCallback( someDelegate);
UnmanagedFuncDo Work();
GC.KeepAlive(so meDelegate);

bye
Rob
Nov 16 '05 #4

According to everything I've read, in my situation, I don't need to be
concerned about GC because the delegates are not cleaned up <used
loosely> until after the unmanaged call returns and the managed
invocation method goes out of scope.

As mentioned earlier, I can get the unmanaged call to run properly, it
just errors out at the end.

Any suggestions?
Joe Martin

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5
Not sure why you pass the this pointer as fourth argument to explode.
Also note that CLR callbacks are __stdcall by default, are you sure Explode
doesn't expect __cdecl callbacks?

Willy.
"Joe Martin" <an*******@devd ex.com> wrote in message
news:eZ******** ******@TK2MSFTN GP15.phx.gbl...
Has anyone made use of PKWare's PKCDL.DLL for the Implode and Explode
functions?

I have been able to successfully define everything correctly and even
get the invoked function (Explode) to run properly. This means data is
being exploded properly, confirmed with CRC values and reviewing the
actual data.

However just before the unmanaged function exits, I get a null exception
error. I am using:

ReadHandler rh = new ReadHandler(thi s.ReadData);
WriteHandler wh = new WriteHandler(th is.WriteData);

IntPtr buffer = Marshal.AllocHG lobal(bufferSiz e)
try {
object data = this;

// Invoke DLL here...
PkCdl.Explode(r h, wh, buffer, ref data);

} finally {
Marshal.FreeHGl obal(buffer);
}

to allocate the needed buffer for the DLL. In the corresponding
ReadData/WriteData callback methods, I use the Marshal.Copy() methods to
move data in/out of managed memory.

Now somewhere along the way an the exception happens inside the
try..finally (shown above).

If this rings a bell with anyone, I will gladly supply complete sample
code and would be more than interested in hearing your thoughts.
Thanks,
Joe Martin

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #6


Okay... Here's more detailed code showing what I'm doing:

public class PkCdl {
public const int FORMAT_MESSAGE_ FROM_SYSTEM = 0x00001000;

[ DllImport( "Kernel32.d ll" )]
public static extern int FormatMessage(i nt flags, IntPtr source, int
messageId, int languageId, StringBuilder buffer, int size, IntPtr
arguments);

[DllImport("PKCD L.DLL", EntryPoint="pkc rc32",
CallingConventi on=CallingConve ntion.Cdecl)]
public static extern int Crc32(IntPtr buffer, UInt32 size, int oldCrc);

[DllImport("PKCD L.DLL", EntryPoint="pke xplode", CharSet=CharSet .Auto,
CallingConventi on=CallingConve ntion.Cdecl, SetLastError=tr ue)]
public static extern UInt32 Explode(ReadHan dler reader, WriteHandler
writer, IntPtr buffer, ref object param);

[DllImport("PKCD L.DLL", EntryPoint="pki mplode", CharSet=CharSet .Auto,
CallingConventi on=CallingConve ntion.Cdecl, SetLastError=tr ue)]
public static extern UInt32 Implode(ReadHan dler reader, WriteHandler
writer, IntPtr buffer, ref object param, DCLDataType cmdType,
DCLDictionarySi ze dsize);

[DllImport("PKCD L.DLL", EntryPoint="pkG etBufferSize",
CallingConventi on=CallingConve ntion.Cdecl)]
public static extern int GetBufferSize(i nt type);

public static string LastWin32Messag e() {
int errCode = Marshal.GetLast Win32Error();
StringBuilder buffer = new StringBuilder(2 56);
FormatMessage(F ORMAT_MESSAGE_F ROM_SYSTEM, IntPtr.Zero, errCode, 0,
buffer, buffer.Capacity , IntPtr.Zero);
return buffer.ToString ();
}
}

public delegate int ReadHandler(Int Ptr buffer, UInt32 size, ref object
param);
public delegate void WriteHandler(In tPtr buffer, UInt32 size, ref object
param);

public UInt32 ReadData(IntPtr buffer, UInt32 size, ref object param) {
try {
byte[] values = archiveReader.R eadBytes(Conver t.ToInt32(size) );
Marshal.Copy(va lues, 0, buffer, values.Length);

return Convert.ToUInt3 2(values.Length );
} catch (Exception ex) {
string msg = ex.Message;
return 0;
}
}

public void WriteData(IntPt r buffer, UInt32 size, ref object param) {
try {
byte[] values = new byte[size];
Marshal.Copy(bu ffer, values, 0, Convert.ToInt32 (size));

outWriter.Write (values); // Write output to where ever

} catch (Exception ex) {
string msg = ex.Message;
}
}

// Actual code invocation
int bufferSize = GetBufferSize(D CLBufferType.PK EXT,
DCLAlgorithmTyp e.DCLIMPLODE);

IntPtr buffer = Marshal.AllocHG lobal(bufferSiz e); // 12596
try {
object data = this;
ReadHandler rh = new ReadHandler(Rea dData);
WriteHandler wh = new WriteHandler(Wr iteData);
UInt32 status = PkCdl.Explode(r h, wh, buffer, ref data);
} finally {
Marshal.FreeHGl obal(buffer);
}


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #7
This PInvoke declaration,

[DllImport("PKCD L.DLL", EntryPoint="pke xplode", CharSet=CharSet .Auto,
CallingConventi on=CallingConve ntion.Cdecl, SetLastError=tr ue)]
public static extern UInt32 Explode(ReadHan dler reader, WriteHandler
writer, IntPtr buffer, ref object param);

- this function passes a reference to an object reference as argument (ref
object param ), I don't see why an unmanaged function would ever need a
reference to an GC object.
All Explode functions I have seen so far only took three arguments, could
you post the corresponding function prototype as declared in the PKWARE
header file.
- ReadHandler and WriteHandler function should be declared using __stdcall
calling convention. Are you sure the functions are declared as such in the
PKWARE headers? If this is not the case, the callbacks are called using a
mismatched calling convention (__stdcall for .NET and __cdecl for unmanaged
C), most possibly corrupting the stack at function call return.

Willy.

"Joe Martin" <an*******@devd ex.com> wrote in message
news:%2******** **********@TK2M SFTNGP14.phx.gb l...


Okay... Here's more detailed code showing what I'm doing:

public class PkCdl {
public const int FORMAT_MESSAGE_ FROM_SYSTEM = 0x00001000;

[ DllImport( "Kernel32.d ll" )]
public static extern int FormatMessage(i nt flags, IntPtr source, int
messageId, int languageId, StringBuilder buffer, int size, IntPtr
arguments);

[DllImport("PKCD L.DLL", EntryPoint="pkc rc32",
CallingConventi on=CallingConve ntion.Cdecl)]
public static extern int Crc32(IntPtr buffer, UInt32 size, int oldCrc);

[DllImport("PKCD L.DLL", EntryPoint="pke xplode", CharSet=CharSet .Auto,
CallingConventi on=CallingConve ntion.Cdecl, SetLastError=tr ue)]
public static extern UInt32 Explode(ReadHan dler reader, WriteHandler
writer, IntPtr buffer, ref object param);

[DllImport("PKCD L.DLL", EntryPoint="pki mplode", CharSet=CharSet .Auto,
CallingConventi on=CallingConve ntion.Cdecl, SetLastError=tr ue)]
public static extern UInt32 Implode(ReadHan dler reader, WriteHandler
writer, IntPtr buffer, ref object param, DCLDataType cmdType,
DCLDictionarySi ze dsize);

[DllImport("PKCD L.DLL", EntryPoint="pkG etBufferSize",
CallingConventi on=CallingConve ntion.Cdecl)]
public static extern int GetBufferSize(i nt type);

public static string LastWin32Messag e() {
int errCode = Marshal.GetLast Win32Error();
StringBuilder buffer = new StringBuilder(2 56);
FormatMessage(F ORMAT_MESSAGE_F ROM_SYSTEM, IntPtr.Zero, errCode, 0,
buffer, buffer.Capacity , IntPtr.Zero);
return buffer.ToString ();
}
}

public delegate int ReadHandler(Int Ptr buffer, UInt32 size, ref object
param);
public delegate void WriteHandler(In tPtr buffer, UInt32 size, ref object
param);

public UInt32 ReadData(IntPtr buffer, UInt32 size, ref object param) {
try {
byte[] values = archiveReader.R eadBytes(Conver t.ToInt32(size) );
Marshal.Copy(va lues, 0, buffer, values.Length);

return Convert.ToUInt3 2(values.Length );
} catch (Exception ex) {
string msg = ex.Message;
return 0;
}
}

public void WriteData(IntPt r buffer, UInt32 size, ref object param) {
try {
byte[] values = new byte[size];
Marshal.Copy(bu ffer, values, 0, Convert.ToInt32 (size));

outWriter.Write (values); // Write output to where ever

} catch (Exception ex) {
string msg = ex.Message;
}
}

// Actual code invocation
int bufferSize = GetBufferSize(D CLBufferType.PK EXT,
DCLAlgorithmTyp e.DCLIMPLODE);

IntPtr buffer = Marshal.AllocHG lobal(bufferSiz e); // 12596
try {
object data = this;
ReadHandler rh = new ReadHandler(Rea dData);
WriteHandler wh = new WriteHandler(Wr iteData);
UInt32 status = PkCdl.Explode(r h, wh, buffer, ref data);
} finally {
Marshal.FreeHGl obal(buffer);
}


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #8


Sure, here you go:
typedef unsigned int(PKCALLBACK * PKReadBuf)(void * buf, unsigned int
size, void* param)

Read buffer call back function definition.
Parameters:
buf Pointer the buffer to fill with data
size The amount of data to read
param User specified call back pointer.

Returns:
The actual amount of data read, must be less than or equal to size.

typedef void(PKCALLBACK * PKWriteBuf)(con st void* buf, unsigned int
size, void* param)

Write buffer call back function definition.
Parameters:
buf Pointer to the buffer containing the data to write
size The size of the buffer
param User specified call back pointer.


//----------------------------------

PKCDL_DECL unsigned int PKCALL pkexplode (
PKReadBuf read_buf,
PKWriteBuf write_buf,
void * work_buf,
void * param
)

Function to decompress data using the DCL implode algorithm.
Parameters:
read_buf Pointer to read buffer function.
write_buf Pointer to the write buffer function.
work_buf Pointer to the working buffer.

Returns:
Returns the error code for the function.
See also:
ERROR_CODES for possible return values

//----------------------------
Hope this helps...
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #9
How would you suggest I go about sync-ing up both the managed delegate
declaration and the unmanaged cdecl of the pkexplode functions?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #10

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

Similar topics

1
370
by: Muthiah Samy | last post by:
Hi All, I am trying to call COM Component in .NET using PInvoke. In the following scenario, ( I am getting an exception called SEH - External component has thrown an exception) UnManaged Code ---------------
2
445
by: RobJUK66 | last post by:
have a 3rd party .Net dll that wraps a set of further unmanaged dll's. The ..Net DLL basically uses pinvoke to wrap the calls. The low level dlls provide an API to talk to a complex device which appears as a USB hub (with a scanner, flash drive and some other bits and pieces). Using a VB.Net windows app, we call the main unmanaged API function (lets call it doStuff() ) and then call messagebox.show to pop up a modal results dialog. This...
2
2995
by: Jeff Lederer | last post by:
I have created a simple test C# console program that calls an unmanaged C subroutine in a DLL where one of the arguments is a callback to the C# code. I noticed that when the callback has no paramaters, it works fine. But when there are parameters, I get the exception: "Runtime failure check #0: The value of ESP not properly saved..." Here are the facts: The delegate declaration: >>> public delegate int querydlg(IntPtr pEnv, string...
3
3470
by: Brett Robichaud | last post by:
I have created a simple background thread to make one pinvoke call into a DLL I've created. My Winforms app spawns the thread in the form load event then go about it's business. The problem is that my form appears to be blocked while the background thread is running. I am very familir with threads in unmanaged code but am just getting into them in C#. Are there issues I need to be aware of with regards to threading a simple pinvoke...
1
1867
by: cppdev | last post by:
Hello, After reading a few articles, http://blogs.gotdotnet.com/cbrumme/PermaLink.aspx/e55664b4-6471-48b9-b360-f0fa27ab6cc0 http://blogs.gotdotnet.com/anathan/commentview.aspx/8ec0b7b2-6290-4500-a8c7-1b6c677214cb i have the following question: __gc class C {
4
1678
by: yaron | last post by:
Hi, I want to use my unmanaged c++ class library from a c# client. my unmanaged c++ class library use polymorpism, is this polymorphism also exported to my c# client via PInvoke ? thanks.
3
16060
by: msnews.microsoft.com | last post by:
Hi i am using User32.dll in Visual stdio 2005. public static extern long SetActiveWindow(long hwnd); public static extern long keybd_event(byte bVk, byte bScan, long dwFlags,
8
6661
by: Rajesh Soni | last post by:
Hi! I'm getting a PInvoke error while trying to execute the following code... declaration: Structure POINTAPI Dim x As IntPtr
14
3800
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain more why C++/CLI would be better to PInvoke than doing the PInvoke in C#? Because, usually in C# as you already know we use DLLImport and extern
0
9795
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, 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...
0
9642
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10780
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, 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...
0
10498
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 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...
1
10540
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,...
1
7753
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 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...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5789
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4421
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

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.