Solved

running an  .lnk file in c++

Posted on 2007-11-28
8
625 Views
Last Modified: 2008-02-01
how do I exec a app.lnk file in compiled code?
0
Comment
Question by:Troudeloup
8 Comments
 
LVL 3

Expert Comment

by:wizrr
ID: 20370371
Use ShellExecuteEx function.
http://msdn2.microsoft.com/en-us/library/bb762154.aspx
http://msdn2.microsoft.com/en-us/library/bb759784.aspx
SHELLEXECUTEINFO info;
ZeroMemory(&info, sizeof(SHELLEXECUTEINFO));
info.cbSize = sizeof(SHELLEXECUTEINFO);
info.lpFile = _T("path to .lnk file");
info.lpVerb = _T("open")
if ( ShellExecuteEx(&info) ) {
  // success
} else {
  // fail
  DWORD dwLastErr = GetLastError();
  // analyse error
}

Open in new window

0
 
LVL 3

Expert Comment

by:wizrr
ID: 20370375
Forget ';' after _T("open")
0
 

Author Comment

by:Troudeloup
ID: 20370389
I think this is the only things i need, i trimed it a little bit


      SHELLEXECUTEINFO info;
      ZeroMemory(&info, sizeof(SHELLEXECUTEINFO));
      info.cbSize = sizeof(SHELLEXECUTEINFO);
      info.lpFile = _T("path to .lnk file");
      info.lpVerb = _T("open");
      ShellExecuteEx(&info);



i got this error.


mouse_ops.cpp:28: error: `_T' was not declared in this scope

0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:wizrr
ID: 20370462
try to include tchar.h file or use Unicode version of function  ShellExecuteExW and SHELLEXECUTEINFOW and L"path to .lnk file" instead of _T("path to .lnk file").
0
 
LVL 4

Expert Comment

by:yuy2002
ID: 20370780
>>mouse_ops.cpp:28: error: `_T' was not declared in this scope
That means your project isn't unicode encoding.
If you use vc6 ,in your project setting->c/c++, add "UNICODE _UNICODE_" to preprocessor definitions .
Otherwise, if you use vc7 or vc8 ,in your project properties,add "UNICODE _UNICODE_" to preprocessor definitions .
Macro UNICODE means that it will support unicode in your own code;
Macro _UNICODE_ means that it will support unicode by (GNU)c lib you linked.
To support unicode , you need to use the function which support unicode.
As wizrr said, ShellExecuteExW and SHELLEXECUTEINFOW  are necessary.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 20371180
>>mouse_ops.cpp:28: error: `_T' was not declared in this scope

All you need to add is

#include <tchar.h>

Or just use no _T() at all:


      SHELLEXECUTEINFO info;
      ZeroMemory(&info, sizeof(SHELLEXECUTEINFO));
      info.cbSize = sizeof(SHELLEXECUTEINFO);
      info.lpFile = "path to .lnk file";
      info.lpVerb = "open";
      ShellExecuteEx(&info);

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20371963
>>>> That means your project isn't unicode encoding.
>>>> #include <tchar.h>
>>>> Or just use no _T() at all:
It makes no sense to push a prog to UNICODE in order to make the _T compile. The _T is a macro that allows to *switch* between ANSI and UNICODE characters. As jkr said you simply have to include the tchar.h or - better - follow his second advice to drop all _T () which rarely have any advantage for the task you are asking for.

Regards, Alex
0
 
LVL 3

Expert Comment

by:wizrr
ID: 20372916
You can try also TEXT( instead of _T(. In any case you can define your own macro

#ifdef UNICODE
#define _UNICODE_OR_ANSI(text) L##text
#else
#define _UNICODE_OR_ANSI(text) ##text
#endif

and use it.

You also can use ansi version of functions ShellExecuteExA and SHELLEXECUTEINFOWA and just "path to .lnk file" instead of _T("path to .lnk file").

When working with _T like macro you should use _tcscmp instead of strcmp and e.g.. But when you using printf function's you must know is your format parameter is ANSI or UNICODE. See



char * g_csFormatBuffer = new char[5048];
 
std::string formatString(char * a_csFormat, ...) {
	va_list param_pt;
 
	va_start(param_pt, a_csFormat);
 
	vsprintf(g_csFormatBuffer, a_csFormat, param_pt);
 
	va_end(param_pt);
 
	return (std::string(g_csFormatBuffer));
}
// this is thread-unsafe function it uses global buffer
 
//if you use function function like this
TCHAR * tcsParam = _T("PARAM");
std::string myStr = formatString("my parameter is %S", tcsParam);
// you will get errors when compiling without using UNICODE
std::string myStr = formatString("my parameter is %s", tcsParam);
// you will get errors when compiling using UNICODE
// ------------
// how to solve? define macro as follows
#ifdef UNICODE
#define AT_FMT "%S"
#else
#define AT_FMT "%s"
#endif
 
#define TAT_FMT _T( AT_FMT )
//and use it as follows:
std::string myStr = formatString("my parameter is " AT_FMT, tcsParam);
// that will compiled normally and works without errors if ANSI or UNCICODE is used

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Least Squares Curve Fitting 4 99
Header of docx file 17 115
gdb doesn't stop on breakpoint 2 94
Issues with C++ Class 19 101
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question