⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 launcher.cpp

📁 关于JAR文件生成EXE文件其实还是要用虚拟机的
💻 CPP
字号:
/*
This is an application that can be used to launch Java 
applications by just clicking on a exe file.

You can rename launcher.exe to anything you want.
*/

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
#include <shellapi.h>

void deletenewlines(char* buffer)
{
  while(*buffer != '\0')
  {
    if (*buffer == '\n')
    {
      *buffer = '\0';
      return;
    }
    ++buffer;
  }
}

void getExecutable(char *result, char* lpCmdLine)
{
  const char* constCommandLine = ::GetCommandLine();
  char* commandLine = new char[strlen(constCommandLine)+1];
  strcpy(commandLine,constCommandLine);
 
  // Remove lpCmdLine from back
  int lastpos = strlen(commandLine)-strlen(lpCmdLine);
  commandLine[lastpos] = '\0';
  lastpos--;
  while(commandLine[lastpos]=='\"' || commandLine[lastpos]==' ')
  {
    commandLine[lastpos]='\0';
    lastpos--;
  }  
  int firstpos = lastpos;
  while(commandLine[firstpos] > 0 && commandLine[firstpos]!='\\' 
    && commandLine[firstpos]!=':' && commandLine[firstpos]!='/')
    firstpos--;

  strcpy(&result[0], &commandLine[firstpos+1]);
  int resultlength = strlen(result);
  
  bool containsdot = false;
  for (int i=0; i<resultlength; i++)
    if (result[i]=='.')
      containsdot = true;

  if (containsdot)
    while(result[resultlength]!='.')
      resultlength--;

  strcpy(&result[resultlength],".cfg");

  resultlength = strlen(result);  
  result[resultlength+1] = '\0';
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  FILE* file = NULL;

  // Look for config file with same name as executable but with cfg suffix.
  char executable[1000];

  getExecutable(executable,lpCmdLine);
  file = fopen(executable,"rt");

  char cfgFileName[1000];
  if (file)
    strcpy(cfgFileName,executable);

  // Open default config file
  if (!file)
  {
    file = fopen("launcher.cfg","rt");
    strcpy(cfgFileName, "launcher.cfg");
  }

  if (file==NULL)
  {
    char temp[1000];
    sprintf(temp,"Could not find file %s or launcher.cfg. Please\n" 
      "reinstall the application.", executable);
    MessageBox(NULL, temp,"Error loading configuration file", MB_OK);
    return 1;
  }

  char buffer[10000];

  // Read new path. Should at the very least include the
  // JRE bin directory and the jvm.dll dir.
  char dir[10000];
  _getcwd(dir,10000);

  // Read new directory from config file. We will change to this dir
  // before starting execution.
  fgets(buffer,10000,file);
  deletenewlines(buffer);
  int result = _chdir(buffer);

  if (result!=0)
  {
    char temp[1000];
    sprintf(temp, "Could not find the directory given in %s\n" \
      "Please reinstall the application.", cfgFileName);
    MessageBox(NULL, temp, "Error changing directory",MB_OK);
    return 2;
  }

  // Read the name of the executable. Is usually java.exe or javaw.exe
  char exe[1000];
  fgets(exe,1000,file);
  deletenewlines(exe);

  // Read any extra parameters you want to give to the program.
  // such as -jar parameters.
  char parameter[1000];
  fgets(parameter,1000,file);
  deletenewlines(parameter);

  // Add parameters given to the exe
  strcat(parameter," ");
  strcat(parameter, lpCmdLine);

  char thecwd[1000];
  _getcwd(thecwd,1000);
  if (ShellExecute(NULL,NULL,exe,parameter,thecwd,SW_SHOWNORMAL)<=(HINSTANCE)32)
  {
    MessageBox(NULL,
      "Could not start the application",
      "Please reinstall the application",MB_OK);
    return 2; 
  }

  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -