[Mono-list] P-Invoke global variable

Justin Cherniak justin.cherniak at gmail.com
Sun Apr 20 18:18:02 EDT 2008


Jerry,

Well, maybe I didn't phrase it correctly.  I have a global variable located
in unmanaged code (an int) and I need to access it from managed code.

This is basically what I want:
----------foo.h--------
void init(); /* Initialized things, among other things sets g_handle to a
valid value*/
void function(int handle);
int g_handle;
void close();

----------foo.cs-------
class foo{
     [DllImport("foo.dll")]
     public static extern void init();

     [DllImport("foo.dll")]
     public static extern void function(int handle);

     [DllImport("foo.dll")]
     public static extern void close();

     // Here is the problem
     [DllImport("foo.dll")]
     public static extern int g_handle;

     private static void main() {
         init();
         function(g_handle);
         close();
     }
}

The problem is importing g_handle.  I don't want to have to add an unmanaged
wrapper (which presumably could include get/set methods for the variable),
so I'm wondering if there is any way to access g_handle from managed code.
I've yet to be able to find one.

Thanks,
Justin

On Sun, Apr 20, 2008 at 5:38 PM, Jerry Houston <jerry at effjayare.net> wrote:

> Justin Cherniak wrote:
>
> > Hey everyone,
> >
> > I was just wondering if there is a way to p-invoke a global variable
> > (located in a dll) without adding any unmanaged code (such as get/set
> > methods)?  I can't seem to figure out how it would work although in theory I
> > don't see why it would be any different than finding a function from the
> > native code as long as it is well defined in a header file.
> >
>
> That's the first time I've ever heard anyone speak of "invoking" a static
> variable.  Usually makes one think of code.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20080420/635b5bd9/attachment-0001.html 


More information about the Mono-list mailing list