Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 12th, 2004, 12:59 PM
Registered User
 
Join Date: Jul 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help. Dllimport issues

Hi guys, I'm having real trouble marshalling Pinvoke data types. I am trying to access an API, and am attempting to convert the C header data into code pivoke
can use. I managed to get it to compile, but am a getting runtime error (see below). Any idea what I am doing wrong?
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at Test.HubAPI.APIWrap.MsgConnect(Int32 lMsgAPIVersion, String lpcszTopic, OPERATOR lpOperator, IntPtr lphContext)
   at Test.HubAPI.MainEntryPoint.Main() in c:\documents and settings\z76546\my documents\hubapi\class1.cs:line 190

This is the C header info:

#define APIFUNCTION int __stdcall


typedef void *MSGHANDLE;

typedef struct /* logon information structure */
        {
        char szOprId[OPERATORLEN + 1];
        char szOprPswd[OPERATORLEN + 1];
        } OPERATOR;
typedef OPERATOR *LPOPERATOR;


MSGAPIFUNCTION MsgConnect(long lMsgAPIVersion, const char *lpcszTopic, LPOPERATOR lpOperator, MSGHANDLE *lphContext);


This is my C# code:

public class APIWrap
{
...
        public const int APIVERSION = 1;
        public IntPtr hCtx;
...
        [StructLayout(LayoutKind.Sequential,Pack=4)]
            public struct OPERATOR
        {
            [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=OPERATORLEN + 1)]
            public string szOprId;
            [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=OPERATORLEN + 1)]
            public string szOprPswd;
        };

[DllImport("C:\\dev\\bin\\msg.dll")]
        public static extern int MsgConnect (int lMsgAPIVersion, string lpcszTopic, OPERATOR lpOperator, IntPtr lphContext);
}


class MainEntryPoint
{
     static void Main()
        {
            int Result;
            string IPAddress = "10.199.156.100:8020";
            APIWrap currentEmployee = new APIWrap();
            APIWrap.OPERATOR opr = new APIWrap.OPERATOR();
            opr.szOprId = "logon";
            opr.szOprPswd = "password";
            Result = APIWrap.MsgConnect(APIWrap.MSG_APIVERSION, IPAddress, opr, currentEmployee.hCtx);
        }
}
 
Old July 12th, 2004, 08:14 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think you're just missing the 'out' modifier on the operator parameter.

In your C header the parameter is an LPOPERATOR (which I think is a pointer, my C is a bit flaky). Where as in you C# version it is just declared OPERATOR which is a value type.

Try...
Code:
[DllImport("C:\\dev\\bin\msg.dll")]
public static extern int MsgConnect(int lMsgApiVersion, string lpcszTopic, out OPERATOR lpOperator, IntPtr lphContext);
You could also try specifying more details for DllImport, like whether the strings are UniCode or ASCII and whether or not SetLastError is called by the external DLL.

The only other thing that I can think which would cause the problem would be declaring your structure with byte arrays rather than strings.
 
Old July 13th, 2004, 07:57 AM
Registered User
 
Join Date: Jul 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Your advice is correct, I added a ref to the opr object and also to hCtx which is a handle. It was defined in the C header file as:

typedef void *PSMSGHANDLE;

So the following code works:

Result = APIWrap.MsgConnect(APIWrap.MSG_APIVERSION, IPAddress, ref opr, ref currentEmployee.hCtx);

Thanks for your help!






Similar Threads
Thread Thread Starter Forum Replies Last Post
dllimport nitesh kumar Windows Presentation Foundation 0 July 16th, 2008 12:58 AM
connection string issues, web.config file issues kaliaparijat ASP.NET 2.0 Professional 1 June 12th, 2008 08:07 AM
IE Issues iPagan BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 April 1st, 2007 04:11 AM
DLLIMPORT Help chris242 C# 2005 0 March 1st, 2006 11:42 AM
For-each issues hiskeyd XSLT 1 February 27th, 2006 08:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.