Carlos Henrique Biazin Esteves Carlos Henrique Biazin Esteves - 9 months ago 152
C# Question

Import MQIC.DLL in C#

In my system, I need to load MQIC.DLL (IBM WebSphere MQ) to send and get messages to a MQ server.

Currently, this service is running in VB5, and I need to upgrade to .Net. When I try to load this library, I get the error:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

The code:

[DllImport("MQIC.DLL", EntryPoint = "MQCONNstd@16", SetLastError = true)]
public static extern void MQCONN(string QMgrName, long Hconn, long CompCode, long Reason);

Does anybody have idea why it's happening?

The VB5 code:

Declare Sub MQCONN Lib "MQIC.DLL" Alias "MQCONNstd@16" (ByVal QMgrName As String, Hconn As Long, CompCode As Long, Reason As Long)


Change the declaration of MQCONN to use out long for all long parameters.

Check MQ application programming reference for each API call, and make sure you use out or ref for every parameter described as output or input/output.

You can also look for cmqc.h in your MQ installation directory, provided you have installed development environment. It has declarations of all MQI calls. You can transcribe the C header to C# - wherever you see a pointer (&) use out or ref.

But generally speaking, you are going to be much better off using managed .Net classes for MQ, as others suggested.