How to get correct code for this DLL?

Hi.

I am a C++ newbie.

I have developped a DLL in C++, using VisualStudio.

Here is the code :

#include <windows.h>
#include <string>

using namespace std;

void __stdcall MYFUNCTION(long anynumber)

{

SHELLEXECUTEINFO command1;
ZeroMemory (&command1, sizeof (SHELLEXECUTEINFO));
command1.cbSize = sizeof (SHELLEXECUTEINFO);
command1.lpVerb = L"open";
command1.lpFile = L"c:\\Programs\\MyProgram.Exe";
command1.lpParameters = L"abcd c:\\tsclose -o c:\\tsclose";
command1.nShow = SW_HIDE;
command1.fMask = SEE_MASK_NOCLOSEPROCESS;
ShellExecuteEx(&command1);
WaitForSingleObject(command1.hProcess, INFINITE);

}

This DLL calls the program MyProgram.exe, passing the parameter
"abcd c:\\tsclose -o c:\\tsclose". The parameter passed to the function, anynumber, is not used.

This works perfectly well, but I would like to get rid of the two hard-coded strings used for command1.lpFile and command1.lpParameters.
I would like to replace those hard-coded strings by two variables passed by the function MYFUNCTION.

Here is the code that I developped :

#include <windows.h>
#include <string>

using namespace std;

void __stdcall MYFUNCTION(LPSTR string1, LPSTR string2)

{

SHELLEXECUTEINFO command1;
ZeroMemory (&command1, sizeof (SHELLEXECUTEINFO));
command1.cbSize = sizeof (SHELLEXECUTEINFO);
command1.lpVerb = L"open";
command1.lpFile = string1;
command1.lpParameters = string2;
command1.nShow = SW_HIDE;
command1.fMask = SEE_MASK_NOCLOSEPROCESS;
ShellExecuteEx(&command1);
WaitForSingleObject(command1.hProcess, INFINITE);

}

(it is assumed that the program calling the DLL would assign the value "c:\\Programs\\MyProgram.Exe" to 'string1' and the value "abcd c:\\tsclose -o c:\\tsclose" to 'string2').

During the Build process, I receive the 2 following error messages :

error C2440: '=' : cannot convert from 'LPSTR' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

error C2440: '=' : cannot convert from 'LPSTR' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


What is wrong in this 2nd code?

Thanks in advance.
This is the classic TCHAR problem.

The strings in the SHELLEXECUTEINFO struct are not char pointers (LPSTR). They are TCHAR pointers (LPTSTR).

These two types are not compatible.

Since you have char pointers (LPSTR), you need to use the CHAR version of the struct, SHELLEXECUTEINFOA (note the A at the end).

You also will need to call the char version of ShellExecuteEx, which is ShellExecuteExA (again note the A at the end):

1
2
3
4
5
6
7
8
9
10
SHELLEXECUTEINFOA command1; // <- note the added A
ZeroMemory (&command1, sizeof (SHELLEXECUTEINFOA));  // <- here too
command1.cbSize = sizeof (SHELLEXECUTEINFOA); // <- here too
command1.lpVerb = "open";  // <- we're using chars, so don't give the L prefix (that's wrong anyway)
command1.lpFile = string1;
command1.lpParameters = string2;
command1.nShow = SW_HIDE;
command1.fMask = SEE_MASK_NOCLOSEPROCESS;
ShellExecuteExA(&command1);  // <- note the added A
WaitForSingleObject(command1.hProcess, INFINITE); 



I've written countless posts on this topic. Long story short:

- "normal" WinAPI functions take TCHAR strings. IE: TEXT("foo")
- "A" WinAPI functions take char strings. IE: "foo"
- "W" WinAPI functions take wchar_t strings. IE: L"foo"


Unless you are using TCHARs, you should not use any of the "normal" WinAPI functions. And instead should use the A/W version depending on what character type you have.

1
2
3
4
5
6
7
// This is wrong because it uses wchar_t strings with a TCHAR function:
MessageBox(NULL, L"Wrong", L"Wrong", MB_OK);

// All of these are correct:
MessageBox(NULL, TEXT("Yes"), TEXT("Yes"), MB_OK); // TCHAR strings, TCHAR function
MessageBoxA(NULL, "Yes", "Yes", MB_OK);  // char strings, char function
MessageBoxW(NULL, L"Yes", L"Yes", MB_OK); // wchar_t strings, wchar_t function 


Don't mix and match.
Hi Disch.

I have modified the DLL in the way you indicated and now it works (build OK, execution OK).

Thanks a lot for the time you spent on my query. Your help was of really great value for me.
Topic archived. No new replies allowed.