Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
723 views
in Technique[技术] by (71.8m points)

c++ - Open windows file/folder properties dialog from C

I have this tiny programm, which is intened to show windows file/folder properties dialog on the specified info.lpFile:

#include <windows.h>

main() {
   SHELLEXECUTEINFO info = {0};

   info.cbSize = sizeof(SHELLEXECUTEINFO);
   info.lpFile = "C:\test.txt";
   info.nShow = SW_SHOW;
   info.fMask = 0x00000000;
   info.lpVerb = "properties";

   ShellExecuteEx(&info);
}

When I compile and execute it, I get the following error message:

Error message

I'm using Win7 and Mingw gcc compiler. Does anybody knows what is wrong with my code? Am I missing something?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

1st of all the code as shown does not properly initialise info.

To fix this change

  SHELLEXECUTEINFO info;

to be

  SHELLEXECUTEINFO info = {0};

2ndly use SEE_MASK_INVOKEIDLIST for SHELLEXECUTEINFO's member fMask.

For your reference: https://msdn.microsoft.com/en-us/library/windows/desktop/bb759784%28v=vs.85%29.aspx

Please note that to see the properties window open, the invoking code must not end immediately. So add something like

  Sleep(10000);

to the end of your test code as shown.


Full code that works for me:

#include <windows.h>

int main(void) 
{
  SHELLEXECUTEINFO info = {0};

  info.cbSize = sizeof info;
  info.lpFile = L"C:\tmp\tmp.txt";
  info.nShow = SW_SHOW;
  info.fMask = SEE_MASK_INVOKEIDLIST;
  info.lpVerb = L"properties";

  ShellExecuteEx(&info);

  Sleep(10000);
}

Build options:

/ZI /nologo /W3 /WX- /Od /Oy- /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Gm /EHsc /RTC1 /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fp"DebugSOxyzConsoleEmpty.pch" /Fa"Debug" /Fo"Debug" /Fd"Debugvc100.pdb" /Gd /TC /analyze- /errorReport:queue 

(Tested with VS2010, running Windows 7)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...