I have read several posts about running a command line in c++, but none of them does what I want : no display of the console in an external application when I use the dllfile.
My problem : I want to use a process that picks files from a database. For example : to get a file XYZ.xyz and copy it to the directory MyDirXYZ, I would use the command line
"MyDataGenerator XYZ.xyz C:\MyDirXYZ".
I use Visual studio
Let 's take the following example to clarify the issue, I am trying to create a directory with "mkdir" in C++ , without using CreateDirectory :
First method :
std::string lDirectory("c:\\MyDummyDir")
std::string lCmd("mkdir " + lDirectory);
system((lCmd).c_str());
It will work , however it displays the console when I run it with a dllfile on Excel .
Second Method ( convertToRightFormat() convert a char to a TChar)
std::string lDirectory("c:\\MyDummyDir")
std::string lCmd("mkdir " + lDirectory);
BOOL _status = TRUE;
DWORD _reply, _code;
STARTUPINFO _si = { sizeof(_si) };
PROCESS_INFORMATION _pi;
TCHAR *_cmd = NULL;
_cmd=convertToRightFormat(lCmd.c_str())
_status &= CreateProcess(NULL, _cmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &_si, &_pi);
This method does not work(not even creating the folder) , and obviously it does not work either for the process "MyDataGenerator".
I tried to use the executable MKDIR.EXE and put it in the commandline. In that case, it works , but still does not work for MyDataGenerator.
Is there a generic to make all command lines work, without showing the console?
Thanks you.
#include <Windows.h> // FreeConsole, system
int main(int argc, char* argv[])
{
system("mkdir newdirectoryname");
FreeConsole();
}
This will result in cmd popping up for a split second though. It closes after that.
If thats not acceptable just target the windows subsystem instead of the console one and make sure to not draw the window.
Related
enter link description here[2][this image shows the error]
I tried this code for block the process before execution.But i got a 100 errors while import a ntddk.h and wdm.h in c++.How to solve it?
Then i got sme erroe like this expected a ')' in my 14 and 22 line of code.
So what should i do for removing the 100 error?
#include <ntstatus.h>
#include<DbgEng.h>
#include<Windows.h>
#include <ntddk.h>
#include <wdm.h>
int main()
{
PEPROCESS process1;
process1 = IoGetCurrentProcess();
HANDLE ProcessId = PsGetCurrentProcessId();
PS_CREATE_NOTIFY_INFO CreateInfo;
PCREATE_PROCESS_NOTIFY_ROUTINE_EX(process1, ProcessId, CreateInfo);
PCUNICODE_STRING ImageFileName;
NTSTATUS CreationStatus;
CreateInfo.CreationStatus = STATUS_ACCESS_DENIED;
ImageFileName = CreateInfo.ImageFileName;
if (ImageFileName == (PCUNICODE_STRING)L"firefox.exe")
{
NTSTATUS result;
result = PsSetCreateProcessNotifyRoutineEx(PCREATE_PROCESS_NOTIFY_ROUTINE_EX(process1, ProcessId, CreateInfo), FALSE);
if (result)
{
printf("blocked");
}
}
return 0;
}
Then i got sme erroe like this expected a ')' in my 14 and 22 line of code.
PCREATE_PROCESS_NOTIFY_ROUTINE_EX(process1, ProcessId, CreateInfo);
result=PsSetCreateProcessNotifyRoutineEx(PCREATE_PROCESS_NOTIFY_ROUTINE_EX(process1, ProcessId, CreateInfo), FALSE);
this is the link for showing my error
Don't mix SDK and DDK headers/libraries in one executable.
If you write a driver, don't include Windows.h. Driver code is not Win32 code.
If you want to create a process in suspended state from another Win32 process, use CREATE_SUSPENDED process creation flag in CreateProcess() (or a similar) Win32 call.
If you want to deny process creation for a particular process from a driver, check this StackOverflow question for the boilerplate code.
Question is I have this button that when clicked I would like it open a file for me inside of a specific executable.
I am a tad rusty on c++ and this is a legacy application using c++ 6.0
built on windows xp.....So any help would be greatly appreciated!
Here is my code cpp
void CJunkView::OnCadkeyButton()
{
CString fileToOpen = "C:\\Documents and Settings\\Administrator\\Desktop\\x.prt";
CString exePath = "C:\\CK19\\Ckwin.exe";
system ("start (exePath), (fileToOpen)");
}
When I click this button it returns this Windows cannot find 'exePath,'.Make sure you typed the name correctly and then try again.
You need to build a string that contains the entire system call and the pass the buffer of that string to system()
Edit:
In response to the comment by IInspectable we could just use the implicit conversion operator operator LPCTSTR()
void CJunkView::OnCadkeyButton()
{
CString fileToOpen = "C:\\Documents and settings\\Administrator\\Desktop\\x.prt";
CString exePath = "C:\\CK19\\Ckwin.exe";
CString cmd = "start " + exePath + ", " + fileToOpen;
system (cmd);
}
I'm using tinyxml through openframeworks which uses cstdio for file access. I can see the example program quite happily create and write files but there is no delete so my plan is to implement remove, but after trying to run this code in my own project it doesn't seem to create a file or notify me of an error.
This code runs as expected on windows, just not on mac osx 10.8.5, no file is generated.
#include <cstdio>
int main(int argc, const char * argv[])
{
bool bClosed = false;
bool bWritten = false;
FILE* testFile;
testFile = fopen(".\\test.xml", "w");
if(testFile)
{
bWritten = fputs("test writing.", testFile);
bClosed = !fclose(testFile);
}
return 0;
}
edit: i now know the file exists as can read from it, i just cant view it in finder, i have hidden files shown, its not found its way into the app's package contents.
On a unix-like system (e.g. Mac OS X and Linux) a Windows path as
".\\test.xml"
should rather be
"./test.xml"
Anyway the simplest solution for this case might just be
"test.xml"
I am trying to get the windows path using Qt and C++. The below code compiles, but not gettting the windows folder path in Qt. The same code works in Visual Studio 2010
wchar_t path[MAX_PATH];
SHGetFolderPath(NULL, CSIDL_WINDOWS, NULL, 0, path);
The below code change seems working:
int const bufferSize = 512;
QScopedPointer<WCHAR> dirPath(new WCHAR[bufferSize]);
ZeroMemory( dirPath.operator ->(), bufferSize);
SHGetFolderPath(NULL, CSIDL_WINDOWS, NULL, 0, dirPath.operator ->());
There isn't a Qt function to do this, but what you are asking could be achieved by reading the environtment variable WINDIR:
QStringList env_list(QProcess::systemEnvironment());
int idx = env_list.indexOf(QRegExp("^WINDIR=.*", Qt::CaseInsensitive));
if (idx > -1)
{
QStringList windir = env_list[idx].split('=');
qDebug() << "Var : " << windir[0];
qDebug() << "Path: " << windir[1];
}
Outputs:
Var : "WINDIR"
Path: "C:\WINDOWS"
QString windowsInstallPath;
#ifdef Q_WS_WIN
QDir d;
if (d.cd("%windir%"))
windowsInstallPath = d.absolutePath();
#endif
if (!windowsInstallPath.isNull())
qDebug() << windowsInstallPath;
else
qDebug() << "Not compiled for Windows";
Should work.
I think another very reasonable way to get the Windows directory would be to get it from the environment passed to the program:
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
qDebug() << env.value("windir");
https://doc.qt.io/qt-5/qprocessenvironment.html
I don't think there is a specific Qt function to do this.
The nearest is QSysinfo which tells you the windows version. However SHGetFolderPath() shoudl work in Qt just as well as any other win API call.
ps In Windows vista-> this is replaced with SHGetKnownFolderPath
Here is a one line solution:
QString winPath = QString::fromUtf8(qgetenv("windir"));
This can also be used for any environment variable. I am not sure if qgetenv is available in Qt4 but it is in Qt5.
If your application is not Terminal Services aware, you may get a different directory under TS environment. Found this out myself today, not that I've ever been bit by %windir% or %SystemRoot% or using the ShGetKnownFolderPath or GetWindowsDirectory APIs.
I've opted for using GetSystemWindowsDirectory which exists Windows 2000 and upward. Microsoft's page for the function is here.
Further explanation by Raymond Chen is here.
Finally, the code...
It's written in Delphi 6. Sorry about that :) It's what I'm coding in at the moment, but if you have code for GetWindowsDirectory in your language, then just a few copy + renames are needed as the function signatures are identical. Note: this code is ANSI (...single byte chars in Delphi 6).
function GetSystemWindowsDirectoryA(lpBuffer: PAnsiChar; uSize: UINT): UINT; stdcall; external kernel32 name 'GetSystemWindowsDirectoryA';
function GetSystemWindowsDirectory: string;
var
buf: array[0..MAX_PATH] of Char;
resultLength: Cardinal;
begin
resultLength := GetSystemWindowsDirectoryA(#buf, SizeOf(buf));
if resultLength = 0 then
RaiseLastOSError;
SetLength(Result, resultLength);
Move(buf, PChar(Result)^, resultLength);
end;
From Visual C++, how do I get the path to the current user's My Documents folder?
Edit:
I have this:
TCHAR my_documents[MAX_PATH];
HRESULT result = SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, my_documents);
However, result is coming back with a value of E_INVALIDARG. Any thoughts as to why this might be?
It depends on how old of a system you need compatibility with. For old systems, there's SHGetSpecialFolderPath. For somewhat newer systems, there's SHGetFolderPath. Starting with Vista, there's SHGetKnownFolderPath.
Here's some demo code that works, at least on my machine:
#include <windows.h>
#include <iostream>
#include <shlobj.h>
#pragma comment(lib, "shell32.lib")
int main() {
CHAR my_documents[MAX_PATH];
HRESULT result = SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, my_documents);
if (result != S_OK)
std::cout << "Error: " << result << "\n";
else
std::cout << "Path: " << my_documents << "\n";
return 0;
}
Use the SHGetFolderPath Windows API function and request CSIDL_MYDOCUMENTS.
Using Visual Studio 2017 with an MFC application under Windows 10 I am using the following code snippet with SHGetKnownFolderPath function to get the current user's Documents folder:
#include <string> // include file for C++ native strings
// . . . other code.
PWSTR ppszPath; // variable to receive the path memory block pointer.
HRESULT hr = SHGetKnownFolderPath(FOLDERID_Documents, 0, NULL, &ppszPath);
std::wstring myPath;
if (SUCCEEDED(hr)) {
myPath = ppszPath; // make a local copy of the path
}
CoTaskMemFree(ppszPath); // free up the path memory block
Note that the documentation has this to say about the path variable usage and the path returned:
ppszPath [out]
Type: PWSTR*
When this method returns, contains the address of a pointer to a
null-terminated Unicode string that specifies the path of the known
folder. The calling process is responsible for freeing this resource
once it is no longer needed by calling CoTaskMemFree. The returned
path does not include a trailing backslash. For example, "C:\Users" is
returned rather than "C:\Users\".
For a list of the FOLDERID_ arguments possible see the MSDN article KNOWN_FOLDER_FLAG enumeration.
Note that CSIDL_PERSONAL will not return the desired folder if the user has changed the default save folder in the Win7 Documents library. To get the right folder, you need to use SHLoadLibraryFromKnownFolder to obtain the IShellLibrary for the Documents library, use IShellLibrary::GetDefaultSaveFolder to get the IShellItem for the library's default save folder, and finally use IShellItem::GetDisplayName to get the folder name.
std::string GetMyDocumentsFolderPath()
{
wchar_t Folder[1024];
HRESULT hr = SHGetFolderPathW(0, CSIDL_MYDOCUMENTS, 0, 0, Folder);
if (SUCCEEDED(hr))
{
char str[1024];
wcstombs(str, Folder, 1023);
return str;
}
else return "";
}
cout<<GetMyDocumentsFolderPath()<<endl;
how about this solution? Its working fine for me.