471,612 Members | 1,381 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,612 software developers and data experts.

"HINSTANCE hInstance" in C#

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,
Nov 26 '05 #1
6 19121
scottt wrote:
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
Nov 26 '05 #2
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=CallingConve ntion.Cdecl)]
static extern bool function(int, int param, ref int ex);

If not how can I get the handle. Thx.

Thanks,


"James Curran" wrote:
scottt wrote:
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

Nov 26 '05 #3
> 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

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

--
Mattias SjŲgren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 26 '05 #4

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.

"Mattias Sjögren" wrote:
> 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

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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 26 '05 #5
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" <sc****@discussions.microsoft.com> wrote in message
news:14**********************************@microsof t.com...

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.

"Mattias Sjögren" wrote:
>> > 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

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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 26 '05 #6
Thanks again Willy. so simple I just over looked the obvious.

"Willy Denoyette [MVP]" wrote:
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" <sc****@discussions.microsoft.com> wrote in message
news:14**********************************@microsof t.com...

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.

"Mattias Sjögren" wrote:

>> > 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
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Nov 28 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.