c# - ShellExecute doesnt work if some parameters are just int type -


i have below code launch .exe (in example notepad.exe). code not working. though there no compilation issues.

    [dllimport("shell32.dll", charset = charset.auto)]     static extern bool shellexecuteex(ref shellexecuteinfo lpexecinfo);      public static void exev()     {         shellexecuteinfo info = new shellexecuteinfo();         info.cbsize = system.runtime.interopservices.marshal.sizeof(info);         info.lpverb = "open";         info.lpfile = "c:\\windows\\notepad.exe";         info.nshow = 5;         info.fmask = 0x440;         info.hwnd = intptr.zero;         shellexecuteex(ref info);     }  }  [structlayout(layoutkind.sequential, charset = charset.auto)] public struct shellexecuteinfo {     public int cbsize;     public uint fmask;     public intptr hwnd;     [marshalas(unmanagedtype.lptstr)]     public string lpverb;     [marshalas(unmanagedtype.lptstr)]     public string lpfile;     [marshalas(unmanagedtype.lptstr)]     public string lpparameters;     [marshalas(unmanagedtype.lptstr)]     public string lpdirectory;     public int nshow;     public int hinstapp;     public int lpidlist;     [marshalas(unmanagedtype.lptstr)]     public string lpclass;     public int hkeyclass;     public uint dwhotkey;     public int hicon;     public int hprocess; } 

i tried below code changed shellexecuteinfo structure , started working. changes have done renamed variables hinstapp,lpidlist,hkeyclass,hicon , hprocess int inptr.

[structlayout(layoutkind.sequential, charset = charset.auto)] public struct shellexecuteinfo {     public int cbsize;     public uint fmask;     public intptr hwnd;     [marshalas(unmanagedtype.lptstr)]     public string lpverb;     [marshalas(unmanagedtype.lptstr)]     public string lpfile;     [marshalas(unmanagedtype.lptstr)]     public string lpparameters;     [marshalas(unmanagedtype.lptstr)]     public string lpdirectory;     public int nshow;     public intptr hinstapp;     public intptr lpidlist;     [marshalas(unmanagedtype.lptstr)]     public string lpclass;     public intptr hkeyclass;     public uint dwhotkey;     public intptr hicon;     public intptr hprocess; } 

i know if can make work int datatype variables. or work intptr only? how different in scenario apart datatype size? because doesn't give me syntax error when use int hinstapp,lpidlist,hkeyclass,hicon , hprocess variables doesn't work.

you need dig windows sdk headers see how large type is. example, 64 bit process, sizeof(hwnd) 8 in c++ , sizeof(int) 4 in c# therefore if use int store hwnd, corrupting memory. same hkey, lpitemidlist, hinstance , hicon. intptr designed kind of platform specific data type size.

the compiler won't warn runtime errors memory corruption.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

jdbc - Not able to establish database connection in eclipse -