J
Jimmy
Hey
Trying to make a program that downloads a file from the Internet and
afterwards installs this file. The file that is downloadet is a cab file,
and i use shellexecuteex to simulate a click on the file, so that i can get
it installed. The problem is that i have to use some win32api calls, and i
cannot get it to work properly. When i start the installation through
shellexecuteex the program just continues and then closes, but i want it to
wait for the installation to be complete.
Ok, so i tried to use MsgWaitForsingleObject, but read about it that if the
executing program generates windows, this was not a good idea. Instead i
should use msgwaitformultipleobjects. Whenever i try to do this i get a
system.nullreferenceexception and the calling function throws a
missingmethodexception. Please can anyone help me getting this to work?
PLEASE
Ive made comments in the following code:
private bool Install_Filer()
{
bool ret;
lab_status.Text = "Installing " + tempsti;
Application.DoEvents();
try
{
Int32 INFINITE;
unchecked {INFINITE = (int)0xFFFFFFFF;}
Int32 retcode;
unchecked {retcode = 746;}
string docname = tempsti;
int nSize = docname.Length * 2 + 2;
IntPtr pData = LocalAlloc(0x40, nSize);
Marshal.Copy(Encoding.Unicode.GetBytes(docname), 0, pData, nSize -
2);
SHELLEXECUTEEX see = new SHELLEXECUTEEX();
see.cbSize = 60;
see.dwHotKey = 0;
see.fMask = 0x00000040;
see.hIcon = IntPtr.Zero;
see.hInstApp = IntPtr.Zero;
see.hProcess = IntPtr.Zero;
see.lpClass = IntPtr.Zero;
see.lpDirectory = IntPtr.Zero;
see.lpIDList = IntPtr.Zero;
see.lpParameters = IntPtr.Zero;
see.lpVerb = IntPtr.Zero;
see.nShow = 0;
see.lpFile = pData;
ShellExecuteEx(see);
//THE FOLLOWING THREE LINES SHOULD DO THE TRICK
IntPtr[] pHandles = new IntPtr[1];
pHandles[0] = see.hProcess;
MsgWaitForMultipleObjects(1, pHandles, false, (uint)INFINITE,
0x007F); //THIS IS WHERE I GET MY NULLREFERENCEEXCEPTION AND THE CALLING
FUNCTION GIVES ME A MISSINGMETHODEXCEPTION
//WaitForSingleObject(see.hProcess,INFINITE); //I TRIED TO DO THIS,
BUT THIS MAKES IT ALL HANG, JUST WAITING FOREVER.
LocalFree(pData); //IM ACTUALLY NOT SURE WHAT THIS IS
ret = true;
}
catch (Exception testfejl)
{
MessageBox.Show(testfejl.ToString());
MessageBox.Show("Error installing " + tempsti);
ret = false;
}
return ret;
}
#region
class SHELLEXECUTEEX
{
public UInt32 cbSize;
public UInt32 fMask;
public IntPtr hwnd;
public IntPtr lpVerb;
public IntPtr lpFile;
public IntPtr lpParameters;
public IntPtr lpDirectory;
public int nShow;
public IntPtr hInstApp;
// Optional members
public IntPtr lpIDList;
public IntPtr lpClass;
public IntPtr hkeyClass;
public UInt32 dwHotKey;
public IntPtr hIcon;
public IntPtr hProcess;
}
[DllImport("coredll")]
extern static int ShellExecuteEx( SHELLEXECUTEEX ex );
[DllImport("coredll")]
extern static IntPtr LocalAlloc( int flags, int size );
[DllImport("coredll")]
extern static void LocalFree( IntPtr ptr );
[DllImport("CoreDll.dll")]
private extern static
Int32 WaitForSingleObject( IntPtr Handle,
Int32 Wait);
[DllImport("Mscorlib.dll")]
private extern static
UInt32 MsgWaitForMultipleObjects(
UInt32 nCount, // number of handles in array
IntPtr[] pHandles, // object-handle array
bool bWaitAll, // wait option
UInt32 dwMilliseconds, // time-out interval
UInt32 dwWakeMask); // input-event type
#endregion
Trying to make a program that downloads a file from the Internet and
afterwards installs this file. The file that is downloadet is a cab file,
and i use shellexecuteex to simulate a click on the file, so that i can get
it installed. The problem is that i have to use some win32api calls, and i
cannot get it to work properly. When i start the installation through
shellexecuteex the program just continues and then closes, but i want it to
wait for the installation to be complete.
Ok, so i tried to use MsgWaitForsingleObject, but read about it that if the
executing program generates windows, this was not a good idea. Instead i
should use msgwaitformultipleobjects. Whenever i try to do this i get a
system.nullreferenceexception and the calling function throws a
missingmethodexception. Please can anyone help me getting this to work?
PLEASE
Ive made comments in the following code:
private bool Install_Filer()
{
bool ret;
lab_status.Text = "Installing " + tempsti;
Application.DoEvents();
try
{
Int32 INFINITE;
unchecked {INFINITE = (int)0xFFFFFFFF;}
Int32 retcode;
unchecked {retcode = 746;}
string docname = tempsti;
int nSize = docname.Length * 2 + 2;
IntPtr pData = LocalAlloc(0x40, nSize);
Marshal.Copy(Encoding.Unicode.GetBytes(docname), 0, pData, nSize -
2);
SHELLEXECUTEEX see = new SHELLEXECUTEEX();
see.cbSize = 60;
see.dwHotKey = 0;
see.fMask = 0x00000040;
see.hIcon = IntPtr.Zero;
see.hInstApp = IntPtr.Zero;
see.hProcess = IntPtr.Zero;
see.lpClass = IntPtr.Zero;
see.lpDirectory = IntPtr.Zero;
see.lpIDList = IntPtr.Zero;
see.lpParameters = IntPtr.Zero;
see.lpVerb = IntPtr.Zero;
see.nShow = 0;
see.lpFile = pData;
ShellExecuteEx(see);
//THE FOLLOWING THREE LINES SHOULD DO THE TRICK
IntPtr[] pHandles = new IntPtr[1];
pHandles[0] = see.hProcess;
MsgWaitForMultipleObjects(1, pHandles, false, (uint)INFINITE,
0x007F); //THIS IS WHERE I GET MY NULLREFERENCEEXCEPTION AND THE CALLING
FUNCTION GIVES ME A MISSINGMETHODEXCEPTION
//WaitForSingleObject(see.hProcess,INFINITE); //I TRIED TO DO THIS,
BUT THIS MAKES IT ALL HANG, JUST WAITING FOREVER.
LocalFree(pData); //IM ACTUALLY NOT SURE WHAT THIS IS
ret = true;
}
catch (Exception testfejl)
{
MessageBox.Show(testfejl.ToString());
MessageBox.Show("Error installing " + tempsti);
ret = false;
}
return ret;
}
#region
class SHELLEXECUTEEX
{
public UInt32 cbSize;
public UInt32 fMask;
public IntPtr hwnd;
public IntPtr lpVerb;
public IntPtr lpFile;
public IntPtr lpParameters;
public IntPtr lpDirectory;
public int nShow;
public IntPtr hInstApp;
// Optional members
public IntPtr lpIDList;
public IntPtr lpClass;
public IntPtr hkeyClass;
public UInt32 dwHotKey;
public IntPtr hIcon;
public IntPtr hProcess;
}
[DllImport("coredll")]
extern static int ShellExecuteEx( SHELLEXECUTEEX ex );
[DllImport("coredll")]
extern static IntPtr LocalAlloc( int flags, int size );
[DllImport("coredll")]
extern static void LocalFree( IntPtr ptr );
[DllImport("CoreDll.dll")]
private extern static
Int32 WaitForSingleObject( IntPtr Handle,
Int32 Wait);
[DllImport("Mscorlib.dll")]
private extern static
UInt32 MsgWaitForMultipleObjects(
UInt32 nCount, // number of handles in array
IntPtr[] pHandles, // object-handle array
bool bWaitAll, // wait option
UInt32 dwMilliseconds, // time-out interval
UInt32 dwWakeMask); // input-event type
#endregion