怎么打包成dll,或者帮我打包也行

时间:2008-05-26 11:10:19   来源:论坛整理  作者:  编辑:chinaitzhe
自己用J做一个任务治理器,其中有些功能不能用J实现,必须用jni调用.dll
下面是要打包成.dll的内容,还有其中我不知道怎么把jstring转成LPWSTR类型,自己先把它转成char*,不知道行不行。
下面是代码打包名为System.dll
下面程序里面的几个方法都是要被导出的,希望写个.def 还有返回类型都是jboolean而函数里返回的都是BOOL行不行
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





#include "stdio.h"

//#include "Winbase.h"

#include "Windows.h"

#include <iostream>

#include "jni.h"

#include "reconserver_SystemDll.h"

JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_newprocess(JNIEnv *, jobject, jstring str){

    STARTUPINFO si;

    PROCESS_INFORMATION pi;

    char* message;

    message=(char*)(*env)->GetStringUTFChars(env, str, NULL);       //把jstring换成char*



    ZeroMemory( &si, sizeof(si) );

    si.cb = sizeof(si);

    ZeroMemory( &pi, sizeof(pi) );



    // Start the child process. 

    if( !CreateProcess( NULL, // No module name (use command line). 

        message, // Command line.                                           //LPWSTR类型

        NULL,             // Process handle not inheritable. 

        NULL,             // Thread handle not inheritable. 

        FALSE,            // Set handle inheritance to FALSE. 

        0,                // 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.

    ) 

    {

        return false;

    }

    //cout<<&pi;

    // Close process and thread handles. 

    CloseHandle( pi.hProcess );

    CloseHandle( pi.hThread );

    return true;

}

JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_killprocess(JNIEnv *, jobject, jint id){



HANDLE         hProcessSnap = NULL; 

    BOOL           bRet      = FALSE; 

    int pid=id;

    hProcessSnap=OpenProcess(PROCESS_ALL_ACCESS,false,pid);

    bRet=TerminateProcess(hProcessSnap,1);

    return bRet;

}

JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_poweroff(JNIEnv *, jobject){

    HANDLE hToken;              // handle to process token 

   TOKEN_PRIVILEGES tkp;       // pointer to token structure 

 

   BOOL fResult;               // system shutdown flag 

 

   // Get the current process token handle so we can get shutdown 

   // privilege. 

 

   if (!OpenProcessToken(GetCurrentProcess(), 

        TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 

      return FALSE; 

 

   // Get the LUID for shutdown privilege. 

 

   LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, 

        &tkp.Privileges[0].Luid); 

 

   tkp.PrivilegeCount = 1;  // one privilege to set    

   tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 

 

   // Get shutdown privilege for this process. 

 

   AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, 

      (PTOKEN_PRIVILEGES) NULL, 0); 

 

   // Cannot test the return value of AdjustTokenPrivileges. 

 

   if (GetLastError() != ERROR_SUCCESS) 

      return FALSE; 

 

   // Display the shutdown dialog box and start the countdown. 

 

   fResult = InitiateSystemShutdown( 

      NULL,    // shut down local computer 

      NULL,   // message for user

      5,      // time-out period 

      FALSE,   // ask user to close apps 

      FALSE);   // reboot after shutdown 

 

   if (!fResult) 

      return FALSE; 

 

   // Disable shutdown privilege. 

 

   tkp.Privileges[0].Attributes = 0; 

   AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, 

        (PTOKEN_PRIVILEGES) NULL, 0); 

 

   return TRUE; 

}



JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_restart(JNIEnv *, jobject){

    HANDLE hToken;              // handle to process token 

   TOKEN_PRIVILEGES tkp;       // pointer to token structure 

 

   BOOL fResult;               // system shutdown flag 

 

   // Get the current process token handle so we can get shutdown 

   // privilege. 

 

   if (!OpenProcessToken(GetCurrentProcess(), 

        TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 

      return FALSE; 

 

   // Get the LUID for shutdown privilege. 

 

   LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, 

        &tkp.Privileges[0].Luid); 

 

   tkp.PrivilegeCount = 1;  // one privilege to set    

   tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 

 

   // Get shutdown privilege for this process. 

 

   AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, 

      (PTOKEN_PRIVILEGES) NULL, 0); 

 

   // Cannot test the return value of AdjustTokenPrivileges. 

 

   if (GetLastError() != ERROR_SUCCESS) 

      return FALSE; 

 

   // Display the shutdown dialog box and start the countdown. 

 

   fResult = InitiateSystemShutdown( 

      NULL,    // shut down local computer 

      NULL,   // message for user

      5,      // time-out period 

      FALSE,   // ask user to close apps 

      TRUE);   // reboot after shutdown 

 

   if (!fResult) 

      return FALSE; 

 

   // Disable shutdown privilege. 

 

   tkp.Privileges[0].Attributes = 0; 

   AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, 

        (PTOKEN_PRIVILEGES) NULL, 0); 

 

   return TRUE; 

}

 


reconserver_SystemDll.h
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





/* DO NOT EDIT THIS FILE - it is machine generated */

#include <jni.h>

/* Header for class reconserver_SystemDll */



#ifndef _Included_reconserver_SystemDll

#define _Included_reconserver_SystemDll

#ifdef __cplusplus

extern "C" {

#endif

/*

 * Class:     reconserver_SystemDll

 * Method:    newprocess

 * Signature: (Ljava/lang/String;)Z

 */

JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_newprocess

  (JNIEnv *, jobject, jstring);



/*

 * Class:     reconserver_SystemDll

 * Method:    killprocess

 * Signature: (I)Z

 */

JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_killprocess

  (JNIEnv *, jobject, jint);



/*

 * Class:     reconserver_SystemDll

 * Method:    poweroff

 * Signature: ()Z

 */

JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_poweroff

  (JNIEnv *, jobject);



/*

 * Class:     reconserver_SystemDll

 * Method:    restart

 * Signature: ()Z

 */

JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_restart

  (JNIEnv *, jobject);



#ifdef __cplusplus

}

#endif

#endif






网友回复:其中jni.h,以及jni_md.h
在jdk/include文件夹下
网友回复:帮助顶,
学习ing~~~~
网友回复:在我的BLOG上的一个贴子,也许能帮助你:
http://turntogo.cn

生成最简单的动态链库函数的方法如下:
(1) 用向导生成一个Win32控制台项目,选择DLL设为动态链接库项目。生成的CPP文件如下:

// MyDll.cpp : 定义 DLL 应用程序的入口点。
//

#include "stdafx.h"

#ifdef _MANAGED
#pragma managed(push, off)
#endif
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}

#ifdef _MANAGED
#pragma managed(pop)
#endif

(2) 然后添加要导出的函数,如:
int __stdcall Add(int a,int b){
return a b;
}

(3) 缺省情况下,项目中没有模块定义文件,需要添加新建项,并指定新建一个模块定义文件(DEF)。新建的模块定义文件只有一行:

LIBRARY "MyDll"

然后再添加如下两行,其中第二行是导出函数名:

EXPORTS
Add

(4) 编译链接后会产生MyDll.DLL文件,其中就有一个导出函数Add。
网友回复:3楼说的基本正确
网友回复:http://hi.baidu.com/你就不懂/blog/item/07296afa380a571ca8d311c5.html
网友回复:mark
study
网友回复:我最近做的最多的就是打包
我的工程里有很多DLL
我有一种格式
需要的话联系我
我给你发个样板
lhj8312@163.com
网友回复:UP
网友回复:http://blog.csdn.net/XXKKFF/archive/2007/03/06/1522632.aspx
vc6 dll基础教程
网友回复:dll的教程网上很多
http://dev.rdxx.com/VC/DLL/
关键字:打包,dll,打包,
上一篇:求一个头文件

相关文章

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面