当我启动.jar文件时:
#include <windows.h>
using namespace std;
int main() {
system("start javaw -splash:someImage.png -jar someFile.jar");
return 0;
}
我也在g ++编译器中定义了-mwindows选项。
在启动画面之前 - 黑色控制台显示几毫秒。有没有解决方法可以避免这种情况?
答案 0 :(得分:5)
使用此:http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx
有一个标志CREATE_NO_WINDOW,将您的命令作为参数传递。
答案 1 :(得分:3)
在您的情况下,system()
函数只执行带有cmd.exe
参数的start ...
。因此黑色控制台窗口属于已启动的cmd
进程。 javaw
程序没有控制台窗口。使用javaw -splash:someImage.png -jar someFile.jar
命令行创建一个新流程就足够了,如Creating Processes页面所述。
答案 2 :(得分:2)
我目前的工作示例基于Ernestas Gruodis和其他人,没有任何警告
#define _WIN32_WINNT 0x0500
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
char cmdline[] = " -jar some.jar";
// Start the child process.
if( !CreateProcess( "javaw.exe", // No module name (use command line)
cmdline, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
CREATE_NO_WINDOW, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return 1;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return 0;
}
使用MinGW编译
g++ test.cpp -o test.exe -Wl,--subsystem,windows
-Wl,--subsystem,windows
表示没有控制台窗口。
答案 3 :(得分:1)
我现在的初步解决方案是:
#include <windows.h>
#include <memory.h>
#include <string.h>
#include <stdio.h>
#include <tchar.h>
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {
STARTUPINFO si;
memset(&si, 0, sizeof (STARTUPINFO));
si.cb = sizeof (STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = FALSE;
PROCESS_INFORMATION pi;
memset(&pi, 0, sizeof (PROCESS_INFORMATION));
// Start the child process.
if (!CreateProcess("C:\\Program Files\\Java\\jre7\\bin\\javaw.exe",
" -jar install.jar", // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
0, // Set handle inheritance to FALSE.
CREATE_NO_WINDOW, // ON VISTA/WIN7, THIS CREATES NO WINDOW
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi)) // Pointer to PROCESS_INFORMATION structure.
{
printf("CreateProcess failed (%d).\n", GetLastError());
return 0;
}
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}
但我收到了警告:
main.cpp:在函数&#39; int WinMain(HINSTANCE,HINSTANCE,LPSTR,int)&#39;: main.cpp:38:16:警告:不推荐将字符串常量转换为 &#39; LPSTR {aka char *}&#39; [-Wwrite串] &amp; pi))//指向PROCESS_INFORMATION结构的指针。
如何在没有警告的情况下正确地做到这一点?
PS - 如果有人想向exe文件添加图标 - here是如何在NetBeans IDE中执行此操作的绝佳说明。只需在resource.rc中输入这些行:
#include "ids.h"
IDI_ICON ICON "nice.ico"
并在ids.h文件中:
#define IDI_ICON 101
这就是全部。