"HINSTANCE hInstance" in C#

G

Guest

I am trying to call into some C++ code from C#. One of the functions requires
a handle to the instance (HINSTANCE hInstance) of the C# program. How do I
access this in C#.

Thanks,
 
J

James Curran

scottt said:
I am trying to call into some C++ code from C#. One of the functions
requires a handle to the instance (HINSTANCE hInstance) of the C#
program. How do I access this in C#.

I believe what you are looking for is:

Process.GetCurrentProcess().Handle
 
G

Guest

Thanks. I think that was it but I have one more question. The C++ code is
doing a LoadLibrary() to get a handle of the DLL being loaded. Is there a way
of getting the handle to the DLL in C# if I am using DllImport as in:

[DllImport("myDLL.Dll", EntryPoint="function",
ExactSpelling=false,CallingConvention=CallingConvention.Cdecl)]
static extern bool function(int, int param, ref int ex);

If not how can I get the handle. Thx.

Thanks,




James Curran said:
scottt said:
I am trying to call into some C++ code from C#. One of the functions
requires a handle to the instance (HINSTANCE hInstance) of the C#
program. How do I access this in C#.

I believe what you are looking for is:

Process.GetCurrentProcess().Handle

--
Truth,
James Curran [erstwhile-MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
M

Mattias Sjögren

No that'll give you a process handle. What you need is something like
Marshal.GetHINSTANCE(typeof(YourType).Module).

The C++ code is
doing a LoadLibrary() to get a handle of the DLL being loaded. Is there a way
of getting the handle to the DLL in C# if I am using DllImport as in:

You can call LoadLibrary from managed code as well if you want but I'm
not sure why you need it in this case.


Mattias
 
G

Guest

What I am doing is trying to porting C++ code to C#.
In the C++ code one of the function calls uses the handle returned from
LoadLibrary() as a parameter. Since I am using the "DLLImport" I don't have
that handl to pass into the function. I am looking for a way in C# to get the
same handle as if I had called LoadLibrary().

Any suggestions on how I would get this.


And just as a follow up to my first question the
"Process.GetCurrentProcess().Handle" is not correct. To get the hWnd I think
I need to do the following:

myForm.Handle.ToInt32()

Thanks.
 
W

Willy Denoyette [MVP]

You'll have to call LoadLibrary to get the module handle.

[DllImport("kernel32.dll")]
static extern IntPtr LoadLibrary(string lpFileName);
IntPtr moduleHandle = LoadLibrary("some.dll");

Note that you may combine LoadLibrary and DllImport for the same module.

[DllImport("some.dll")]
static extern int f(..);
....
IntPtr moduleHandle = LoadLibrary("some.dll"); //load some.dll
int i = f(..); // this call will attach to the already loaded dll


Willy.
 
G

Guest

Thanks again Willy. so simple I just over looked the obvious.

Willy Denoyette said:
You'll have to call LoadLibrary to get the module handle.

[DllImport("kernel32.dll")]
static extern IntPtr LoadLibrary(string lpFileName);
IntPtr moduleHandle = LoadLibrary("some.dll");

Note that you may combine LoadLibrary and DllImport for the same module.

[DllImport("some.dll")]
static extern int f(..);
....
IntPtr moduleHandle = LoadLibrary("some.dll"); //load some.dll
int i = f(..); // this call will attach to the already loaded dll


Willy.


scottt said:
What I am doing is trying to porting C++ code to C#.
In the C++ code one of the function calls uses the handle returned from
LoadLibrary() as a parameter. Since I am using the "DLLImport" I don't
have
that handl to pass into the function. I am looking for a way in C# to get
the
same handle as if I had called LoadLibrary().

Any suggestions on how I would get this.


And just as a follow up to my first question the
"Process.GetCurrentProcess().Handle" is not correct. To get the hWnd I
think
I need to do the following:

myForm.Handle.ToInt32()

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top