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 13th, 2004, 01:21 PM
Registered User
 
Join Date: Jul 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Pointers & dereferencing in C#

I am wrapping a Dll function with managed code and am confused about the equivalent of pointers and the & symbol in C. I am working with two C functions wrapped in a Dllimport:

[DllImport("msg.dll",CharSet=CharSet.Ansi)]
public static extern int MsgConnect (int lAPIVersion, string lpcszTopic, ref OPERATOR lpOperator, ref IntPtr lphContext);

Where the C functional prorotype was:
MSGAPIFUNCTION MsgConnect(long lMsgAPIVersion, const char *lpcszTopic, LPOPERATOR lpOperator, MSGHANDLE *lphContext);

The lphContext is a handle, specified in C as:
typedef void *MSGHANDLE

I am trying to import this function:
MSGAPIFUNCTION MsgStartMessage(MSGHANDLE hContext, const char *lpcszActivityName, const char *lpcszMsgName, int bOriginatorVerified)

I have defined it as:
[DllImport("msg.dll",CharSet=CharSet.Ansi)]
public static extern int MsgStartMessage (ref IntPtr hContext, ref string lpcszActivityName, ref string lpcszMsgName, int bOriginatorVerified);

Note it is using the variable itself, not a pointer to it like the previous prototype. I implemented the pointer as IntPtr, not sure how to implement this - do I need to convert it to an Int?

 
Old July 13th, 2004, 06:34 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just so I understand you right. The "MSGHANDLE *lphContext" parameter is a) used by the external function and/or b) set by the external function?

I think from what you're saying is that it is set by the function. You connect to the Message and it returns a handle to it if successful.

I've never done this before but I think what you need to do is allocate a piece of memory for the pointer to be stored in. So I suggest removing the 'ref' modifier on the lphContext parameter and just have it as an IntPtr.

Before calling the function you need to create the memory for the new handle.

Code:
IntPtr lphContext = Marshal.AllocHGlobal(IntPtr.Size);
IntPtr actualHandle;

Marshal.WriteIntPtr(lphContext, IntPtr.Zero);
MsgConnect(/*ver*/1, /*topic*/"blah", /*operator*/someOperator, lphContext);
// if successful
actualHandle = Marshal.ReadIntPtr(lphContext);
Basically this creates a block of memory for the handle and POINTS lphContext at that block. You can then read and write from that block using ReadIntPtr and WriteIntPtr.

If I've understood correctly this should solve your problem.

P.S. Don't forget to remove the "ref" from the lphContext parameter declaration.

Kep.





Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ Pointers reality_42000 C++ Programming 1 March 4th, 2007 01:26 AM
Linux & KDE & C++ & QT & MYSQL & Kdevelop Munnnki Linux 0 January 2nd, 2005 05:41 PM
pointers Stuby085 Visual C++ 1 August 30th, 2003 11:58 PM
Pointers and Arrays C++ odie All Other Wrox Books 1 July 10th, 2003 01:45 PM
Pointers jake VB How-To 12 June 21st, 2003 11:30 AM





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