to 名草: 我的意思是,比如我现在我要在12:00运行我机器上的PhotoShop,于是我就把这些信息指定给我的java程序,我的java程序运行后,就会把我指定的信息写入到NT 的Task Scheduler中,NT接受到信息,到了12:00,就会运行PhotoShop
问题是如何写到NT Task Scheduler里。
我有C/C++的方法: This example attempts to run an existing task. It assumes that the task, Test Task, already exists on the local computer.#include <windows.h> #include <initguid.h> #include <ole2.h> #include <mstask.h> #include <msterr.h> #include <wchar.h> #include<stdio.h> int main(int argc, char **argv) { HRESULT hr = ERROR_SUCCESS; ITaskScheduler *pITS;
/////////////////////////////////////////////////////////////////// // Call CoInitialize to initialize the COM library and then // CoCreateInstance to get the Task Scheduler object. /////////////////////////////////////////////////////////////////// hr = CoInitialize(NULL); if (SUCCEEDED(hr)) { hr = CoCreateInstance(CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER, IID_ITaskScheduler, (void **) &pITS); if (FAILED(hr)) { CoUninitialize(); return 1; } } else { return 1; }
/////////////////////////////////////////////////////////////////// // Call ITaskScheduler::Activate to get the Task object. ///////////////////////////////////////////////////////////////////
(程序定时执行?!~~这里涉及到jni调用NT task scheduler的问题,以前没碰到过此类问题,还请指教!)
我的意思是,比如我现在我要在12:00运行我机器上的PhotoShop,于是我就把这些信息指定给我的java程序,我的java程序运行后,就会把我指定的信息写入到NT 的Task Scheduler中,NT接受到信息,到了12:00,就会运行PhotoShop
This example attempts to run an existing task. It assumes that the task, Test Task, already exists on the local computer.#include <windows.h>
#include <initguid.h>
#include <ole2.h>
#include <mstask.h>
#include <msterr.h>
#include <wchar.h>
#include<stdio.h>
int main(int argc, char **argv)
{
HRESULT hr = ERROR_SUCCESS;
ITaskScheduler *pITS;
///////////////////////////////////////////////////////////////////
// Call CoInitialize to initialize the COM library and then
// CoCreateInstance to get the Task Scheduler object.
///////////////////////////////////////////////////////////////////
hr = CoInitialize(NULL);
if (SUCCEEDED(hr))
{
hr = CoCreateInstance(CLSID_CTaskScheduler,
NULL,
CLSCTX_INPROC_SERVER,
IID_ITaskScheduler,
(void **) &pITS);
if (FAILED(hr))
{
CoUninitialize();
return 1;
}
}
else
{
return 1;
}
///////////////////////////////////////////////////////////////////
// Call ITaskScheduler::Activate to get the Task object.
///////////////////////////////////////////////////////////////////
ITask *pITask;
LPCWSTR lpcwszTaskName;
lpcwszTaskName = L"Test Task";
hr = pITS->Activate(lpcwszTaskName,
IID_ITask,
(IUnknown**) &pITask);
pITS->Release();
if (FAILED(hr))
{
wprintf(L"Failed calling ITaskScheduler::Activate; error = 0x%x\n",hr);
CoUninitialize();
return 1;
}
///////////////////////////////////////////////////////////////////
// Call ITask::Run to start execution of "Test Task".
///////////////////////////////////////////////////////////////////
hr = pITask->Run();
if (FAILED(hr))
{
wprintf(L"Failed calling ITask::Run, error = 0x%x\n",hr);
CoUninitialize();
return 1;
}
pITask->Release();
CoUninitialize();
return 0;
}
1.编写java调用程序
/* NativeTest.java */
import java.lang.*;public class NativeTest {
public native boolean test();
static {
System.loadLibrary("NativeTest");
}
}
2.生成对应.h头文件
javac NativeTest.java
javah -jni NativeTest // 生成 .h 文件
应该生成下面的NativeTest.h
/*
* Class: MyNative
* Method: showParms0
* Signature: (Ljava/lang/String;IZ)V
*/
JNIEXPORT void JNICALL Java_NativeTest_test();
3.编写NativeTest.c文件
#include <stdio.h>
#include "NativeTest.h"
JNIEXPORT void JNICALL Java_NativeTest_test()
{
//...你的代码或调用
}
4.编译NativeTest.c
// cl -I你的java路径\include -I你的java路径\include\win32 -LD NativeTest.c -FeNativeTest.dll 我的java路径 F:\jdk1.3.1
cl -If:\jdk1.3.1\include -If:\jdk1.3.1\include\win32 -LD NativeTest.c -FeNativeTest.dll
编译成连接库的形式,这样就可以了
...
NativeTest n = new NativeTest();
n.test();
...
你上面的是用本地方法运行java程序,我要的是用java往NT Task Scheduler里写
public native boolean test(String command);
调用
java
...
String command = "Test Task";
NativeTest n = new NativeTest(command);
n.test();
...
在你的c/c++程序中接受这个命令command,用c/C++的方法调用(既然你会用c/c++实现)
thisTask.exec("at 12:00 E:\\PhotoShop.EXE");
过程。jni就是完成java不能或不好(效率问题)实现的功能,就像与系统的互动等应用
我谈到的是
程序定时执行?!~~这里涉及到jni调用NT task scheduler的问题,以前没碰到过此类问题,还请指教!)
thisTask.exec("at 12:00 E:\\PhotoShop.EXE");