Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 12th, 2004, 12:59 PM
Registered User
 
Join Date: Jul 2004
Location: , , .
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);
        }
}
Reply With Quote
  #2 (permalink)  
Old July 12th, 2004, 08:14 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Location: Melbourne, VIC, Australia.
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.
Reply With Quote
  #3 (permalink)  
Old July 13th, 2004, 07:57 AM
Registered User
 
Join Date: Jul 2004
Location: , , .
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!

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:16 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.