473,387 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 19643
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: heinquoi | last post by:
hello, i have the code: class window { static const HWND hWnd; static const HINSTANCE hInst; // ... next code here window (HINSTANCE ); }
4
by: Joey | last post by:
Hi, I wrote a mixed-mode dll (with MFC and C++/CLI) which is called from a C#-EXE. Under special cirumstances (that is: another process sends a windows-message to my process - this message is...
1
by: Chris Sharrard | last post by:
Hello All, I have been trying to fix the following problem; this bit of code works fine when it is compiled as part of a Windows application but when I move it out to a DLL I get the debug error -...
2
by: DelphiCoder | last post by:
I have the following function declared: void RegisterMyClass(HINSTANCE hinst, WNDPROC wproc, char* clsName) My window procedure is defined as follows: LRESULT...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.