BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

FindHInstance();

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Grety
Guest





PostPosted: Sun Mar 27, 2005 7:34 pm    Post subject: FindHInstance(); Reply with quote



Hi
There is a exe file which shows Hello World named : KLAK.exe
in RC file I wrote :
IDW Exe "klak.exe"
In my Form1->Button1Click I wrote :

void __fastcall TForm1::Button1Click(TObject *Sender)
{
HINSTANCE h=FindHInstance();
HRSRC hr=FindResource(h,"IDW","Exe");
HGLOBAL hg=LoadResource(h,hr);
LPSTR lp=(LPSTR)LockResource(hg);
system("klak.exe");
FreeResource(hg);
return 0;
}

But I know its not working
first of alll what should I write in FindHInstance(); pranthesis?

The Project is about execute a exe file which is inside another exe file :-)


Back to top
Ed Mulroy [TeamB]
Guest





PostPosted: Mon Mar 28, 2005 12:32 am    Post subject: Re: FindHInstance(); Reply with quote



I have never heard of a function named FindHinstance.

If you place this at the top of the source file
extern HINSTANCE _hInstance;

The variable _hInstance is the program's instance handle, it's
HINSTANCE. The variable is created and initialized by the compiler's
startup code.

.. Ed

"Grety" <valid (AT) emailaddress (DOT) com> wrote

Quote:
Hi
There is a exe file which shows Hello World named : KLAK.exe
in RC file I wrote :
IDW Exe "klak.exe"
In my Form1->Button1Click I wrote :

void __fastcall TForm1::Button1Click(TObject *Sender)
{
HINSTANCE h=FindHInstance();
HRSRC hr=FindResource(h,"IDW","Exe");
HGLOBAL hg=LoadResource(h,hr);
LPSTR lp=(LPSTR)LockResource(hg);
system("klak.exe");
FreeResource(hg);
return 0;
}

But I know its not working
first of alll what should I write in FindHInstance(); pranthesis?

The Project is about execute a exe file which is inside another exe
file Smile



Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Mar 28, 2005 6:16 am    Post subject: Re: FindHInstance(); Reply with quote




"Ed Mulroy [TeamB]" <dont_email_me (AT) invalid (DOT) com> wrote


Quote:
I have never heard of a function named FindHinstance.

It is defined in the VCL's System unit:

Returns the instance handle of the module that contains a specified
address.

extern PACKAGE long __fastcall FindHInstance(void * Address);

Quote:
If you place this at the top of the source file
extern HINSTANCE _hInstance;

The variable _hInstance is the program's instance handle, it's
HINSTANCE. The variable is created and initialized by the
compiler's startup code.

Use the global HInstance variable instead.


Gambit



Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Mar 28, 2005 6:29 am    Post subject: Re: FindHInstance(); Reply with quote


"Grety" <valid (AT) emailaddress (DOT) com> wrote


Quote:
in RC file I wrote :
IDW Exe "klak.exe"

Change that to the following:

IDW RCDATA "klak.exe"

Quote:
In my Form1->Button1Click I wrote :

Change that to the following instead:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
HMODULE hm = (HMODULE) HInstance;
HRSRC hr = FindResource(hm, "IDW", RT_RCDATA);
if( hr )
{
DWORD dwSize = SizeOfResource(hm, hr);
HGLOBAL hg = LoadResource(hm, hr);
if( hg )
{
LPVOID lp = LockResource(hg);
if( lp )
{
HANDLE hFile = CreateFile(".\klak.exe", GENERIC_WRITE,
FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if( hFile != INVALID_HANDLE_VALUE )
{
DWORD dwWritten = 0;
WriteFile(hFile, lp, dwSize, &dwWritten, NULL);
CloseHandle(hFile);

if( dwWritten == dwSize )
WinExec(".\klak.exe", SW_SHOW);
}
}
}
}
}


Which can then be simplified to the following:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
TResourceStream *RS = new TResourceStream(HInstance, "IDW",
RT_RCDATA);
try
{
TFileStream *FS = new TFileStream(".\klak.exe", fmCreate);
try {
FS->CopyFrom(RS, 0);
}
__finally {
delete FS;
}
}
__finally {
delete RS;
}
WinExec(".\klak.exe", SW_SHOW);
}


Gambit



Back to top
Grety
Guest





PostPosted: Wed Mar 30, 2005 8:10 pm    Post subject: Re: FindHInstance(); Reply with quote

Its working well thanks.
But I have to change the lines to the new one by O => o :

DWORD dwSize = SizeOfResource(hm, hr);

DWORD dwSize = SizeofResource(hm, hr);

Thanks for your help
I'm doing my project well.


Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.