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)

This is MQOD struct:

public struct MQOD
{
    public String StrucId; //Structure identifier'
    public long Version; //Structure version number'
    public long ObjectType; //Object type'
    public string ObjectName; //Object name'
    public string ObjectQMgrName; //Object queue manager name'
    public string DynamicQName; //Dynamic queue name'
    public string AlternateUserId; //Alternate user identifier'
}

Upvotes: 0

Views: 1805

Answers (2)

Yuri Steinschreiber
Yuri Steinschreiber

Reputation: 2698

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.

Upvotes: 1

Shashi
Shashi

Reputation: 15273

IBM MQ provides a .NET interface. I recommend you to use that instead of MQIC DLL. http://www.ibm.com/support/knowledgecenter/SSFKSJ_9.0.0/com.ibm.mq.dev.doc/q029250_.htm. There are many samples shipped with the product.

Upvotes: 1

Related Questions