www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - ShellExecute

reply Serj Makaroff <serj-makaroff yandex.ru> writes:
Hello!

I try to use WinAPI function ShellExecute() to start web browser in new
process. I wrote such definition
export {
HINSTANCE ShellExecute(HWND hwnd,LPCTSTR lpOperation,LPCTSTR lpFile,LPCTSTR
lpParameters,LPCTSTR lpDirectory,INT nShowCmd);
}

and called function in such way:

HINSTANCE i = ShellExecute(null, mode, url, null, null, SW_SHOW);

Also I found in MSDN, that this function is in shell32.dll. I added

pragma(lib, "shell32.lib");

I obtain linker error "Symbol Undefined..." Help, pls
Feb 11 2008
parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Mon, 11 Feb 2008 19:18:27 +0100, Serj Makaroff  =

<serj-makaroff yandex.ru> wrote:

 Hello!

 I try to use WinAPI function ShellExecute() to start web browser in ne=
w
 process. I wrote such definition
 export {
 HINSTANCE ShellExecute(HWND hwnd,LPCTSTR lpOperation,LPCTSTR  =
 lpFile,LPCTSTR
 lpParameters,LPCTSTR lpDirectory,INT nShowCmd);
 }

 and called function in such way:

 HINSTANCE i =3D ShellExecute(null, mode, url, null, null, SW_SHOW);

 Also I found in MSDN, that this function is in shell32.dll. I added

 pragma(lib, "shell32.lib");

 I obtain linker error "Symbol Undefined..." Help, pls
When changing the definition to extern (Windows) { HINSTANCE ShellExecuteA(HWND, LPCSTR, LPCSTR, LPCSTR, LPCSTR, INT); HINSTANCE ShellExecuteW(HWND, LPCWSTR, LPCWSTR, LPCWSTR, LPCWSTR, INT);= } this works for me.
Feb 11 2008
parent reply Serj Makaroff <serj-makaroff yandex.ru> writes:
Thank you! It's work.

But why ShellExecute() doesn't work and what is the difference between
ShellExecuteA() and ShellExecuteW()?
Feb 11 2008
next sibling parent reply Sergey Gromov <snake.scaly gmail.com> writes:
Serj Makaroff <serj-makaroff yandex.ru> wrote:
 Thank you! It's work.
 
 But why ShellExecute() doesn't work and what is the difference between
 ShellExecuteA() and ShellExecuteW()?
There is no such Windows function like ShellExecute(). There is ShellExecuteA() which accepts ASCII (CHAR/CHAR*) parameters and returns compatible results, and ShellExecuteW() which accepts Unicode (WCHAR/WCHAR*) parameters and returns accordingly. The ShellExecute is a macro defined in windows.h for C preprocessor, there is no such symbol in the OS. This macro expands to either of the former functions depending on whether the _UNICODE macro is defined. This holds true for most of Windows API. This reminds me: why all the standard D headers declare Windows functions as extern(Windows) export void SomeApiFunction() ? What's the use for export attribute ? -- SnakE
Feb 11 2008
parent reply doob <doobnet gmail.com> writes:
Sergey Gromov wrote:
 Serj Makaroff <serj-makaroff yandex.ru> wrote:
 Thank you! It's work.

 But why ShellExecute() doesn't work and what is the difference between
 ShellExecuteA() and ShellExecuteW()?
There is no such Windows function like ShellExecute(). There is ShellExecuteA() which accepts ASCII (CHAR/CHAR*) parameters and returns compatible results, and ShellExecuteW() which accepts Unicode (WCHAR/WCHAR*) parameters and returns accordingly. The ShellExecute is a macro defined in windows.h for C preprocessor, there is no such symbol in the OS. This macro expands to either of the former functions depending on whether the _UNICODE macro is defined. This holds true for most of Windows API. This reminds me: why all the standard D headers declare Windows functions as extern(Windows) export void SomeApiFunction() ? What's the use for export attribute ?
From the language specification: "Export means that any code outside the executable can access the member. Export is analogous to exporting definitions from a DLL." Tango doesn't use export with win api functions
Feb 11 2008
parent reply Sergey Gromov <snake.scaly gmail.com> writes:
doob <doobnet gmail.com> wrote:
 Sergey Gromov wrote:
 This reminds me: why all the standard D headers declare Windows 
 functions as
 
 extern(Windows) export void SomeApiFunction() ?
 
 What's the use for export attribute ?
 
From the language specification: "Export means that any code outside the executable can access the member. Export is analogous to exporting definitions from a DLL." Tango doesn't use export with win api functions
Thanks. I did know what the specs were saying, I was just wondering if I missed something. I also know that it does work without `export'--- because I've tried. Is it a left-over from an earlier versions of the language ? The example-driven programmers keep copying this code around, making it sort of common practice... -- SnakE
Feb 12 2008
next sibling parent reply torhu <no spam.invalid> writes:
Sergey Gromov wrote:
  > Thanks.  I did know what the specs were saying, I was just wondering if
 I missed something.  I also know that it does work without `export'---
 because I've tried.  Is it a left-over from an earlier versions of the 
 language ?  The example-driven programmers keep copying this code 
 around, making it sort of common practice...
 
I guess you're supposed to use it where you'd use __declspec(dllimport) in C, but it I'm not sure why it works anyway for functions. It's possible that it's not needed since a function prototype can never be a definiton. So it's safe for the linker to resolve it to a function in the import library (.lib). Unless the function is defined in another module you're linking with. Not sure what happens then, maybe you just get that function instead of the one in the dll. It's needed when linking to variables that are in a DLL, though. If you don't add it, even variable declarations with the 'extern' storage class will turn into definitions.
Feb 12 2008
parent reply Sergey Gromov <snake.scaly gmail.com> writes:
torhu <no spam.invalid> wrote:
 It's needed when linking to variables that are in a DLL, though.  If you 
 don't add it, even variable declarations with the 'extern' storage class 
 will turn into definitions.
This explains it, thanks. This also means that export is sometimes also import because extern is not actually for an extern symbol but merely a calling convention specifier. I can live with that. :) -- SnakE
Feb 16 2008
parent torhu <no spam.invalid> writes:
Sergey Gromov wrote:
 torhu <no spam.invalid> wrote:
 It's needed when linking to variables that are in a DLL, though.  If you 
 don't add it, even variable declarations with the 'extern' storage class 
 will turn into definitions.
This explains it, thanks. This also means that export is sometimes also import because extern is not actually for an extern symbol but merely a calling convention specifier.
This guide mostly explains it: http://www.digitalmars.com/d/2.0/htomodule.html And yes, 'export' is used for imports too. Just 'extern' works like in C, while 'extern (XYZ)' is really an unrelated feature.
Feb 16 2008
prev sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Sergey Gromov" <snake.scaly gmail.com> wrote in message 
news:MPG.221c192f273aa432989691 news.digitalmars.com...

 Thanks.  I did know what the specs were saying, I was just wondering if
 I missed something.  I also know that it does work without `export'---
 because I've tried.  Is it a left-over from an earlier versions of the
 language ?  The example-driven programmers keep copying this code
 around, making it sort of common practice...
I think it's used when exporting a symbol _from_ a library/app (i.e. as a symbol to export from a DLL, which does actually work), but when dealing with external symbols, I'm not entirely sure what it's supposed to do. I suppose if it works fine without it, there's no need for it. It might just be one of those cases where D ignores spurious protection attributes.
Feb 12 2008
prev sibling parent Matti Niemenmaa <see_signature for.real.address> writes:
Serj Makaroff wrote:
 But why ShellExecute() doesn't work and what is the difference between
 ShellExecuteA() and ShellExecuteW()?
ShellExecute is a macro, #defined somewhere in windows.h as either ShellExecuteA or ShellExecuteW depending on whether you're using Unicode or not. Many Windows functions are similar #defines. If something doesn't link out of the box, try appending A or W (whichever is the one you want): most of the time it solves the problem. -- E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi
Feb 11 2008