I believe your problem is that you are assuming that the child process should try to load its resources from the current directory (as suggested in your third paragraph) when in fact it is much more usual for a process to load its resources from the directory it is launched from. In other words, the behaviour you are describing is as-expected in most cases.
When an application is launched by double-clicking on a document or by using drag-and-drop, the current directory is set to the directory containing the document, so if the application loads its resources from the current directory, it won't work.
This simple test application demonstrates that lpCurrentDirectory
works, in that the current directory of the child process is set to the directory specified:
#include <Windows.h>
void showcd(wchar_t * caption)
{
wchar_t buffer[512];
if (GetCurrentDirectory(512, buffer) == 0)
{
DWORD err = GetLastError();
MessageBox(NULL, L"GetCurrentDirectory failed", caption, MB_OK);
ExitProcess(err);
}
buffer[511] = L'\0';
MessageBox(NULL, buffer, caption, MB_OK);
}
void parent(wchar_t * cd)
{
wchar_t cmd[512];
STARTUPINFO sinfo;
PROCESS_INFORMATION pinfo;
GetStartupInfo(&sinfo);
showcd(L"Parent Process");
if (GetModuleFileName(NULL, cmd, 512) == 0)
{
MessageBox(NULL, L"GetModuleFileName failed", L"Parent Process", MB_OK);
ExitProcess(GetLastError());
}
cmd[511] = L'\0';
if (!CreateProcess(
cmd, NULL, NULL, NULL, FALSE, 0, NULL, cd, &sinfo, &pinfo
))
{
DWORD err = GetLastError();
MessageBox(NULL, L"CreateProcess failed", L"Oops", MB_OK);
ExitProcess(err);
}
}
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
)
{
wchar_t * cmdline;
for (cmdline = GetCommandLine(); *cmdline; cmdline++)
{
if (*cmdline == L'*')
{
parent(cmdline + 1);
return 0;
}
}
showcd(L"Child Process");
return 0;
}
To test the application, run it with a command line like this:
currentdirectorytest *c:\Users\Public
The first dialog box, from the parent process, shows the parent process current directory. The second dialog box, from the child process, shows the child process current directory and should be the directory given on the command line. (Note that the directory specified must exist, or creation of the child process will fail.)