2 messages in net.java.dev.jna.usersRe: [jna-users] COM Object / ole32
FromSent OnAttachments
Dale...@coats.comApr 4, 2008 9:48 am 
Timothy WallApr 7, 2008 10:54 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] COM Object / ole32Actions...
From:Timothy Wall (twal@dev.java.net)
Date:Apr 7, 2008 10:54:19 am
List:net.java.dev.jna.users

This would probably be easier using JACOB, JAWIN, or com4j, since those can also auto-generate a COM interface in Java from a type library.

On Apr 4, 2008, at 12:48 PM, Dale@coats.com wrote:

I'm not sure if JNA will be good for what I'm trying to do: Create a Windows scheduled task. Should this be 'easy'?

I learned that the first thing I need to do is to initialize a COM object (this page is where I'm getting that info: http://msdn2.microsoft.com/en-us/library/aa446826(VS.85).aspx )

HRESULT CoInitialize( LPVOID pvReserved );

Then, to create the task scheduler object:

STDAPI CoCreateInstance( REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID riid, LPVOID * ppv );

So, hey, what do I know? I just created an Ole32 interface:

public interface Ole32 extends W32API { Ole32 INSTANCE = (Ole32)Native.loadLibrary("ole32", Ole32.class, DEFAULT_OPTIONS);

HANDLE CoInitialize( Object pvReserved);

HANDLE CoCreateInstance( String rclsid, Object pUnkOuter, Integer dwClsContext, String riid, Pointer ppv); }

And tried to use it:

Ole32 ole32 = Ole32.INSTANCE;

Object pvReserved = null; ole32.CoInitialize(pvReserved);

String CLSID_CTaskScheduler = "{148BD52A-A2AB-11CE- B11F-00AA00530503}"; Object pUnkOuter = null; Integer CLSCTX_INPROC_SERVER = new Integer(0x00000001); String IID_ITaskScheduler = "{148BD527-A2AB-11CE-B11F-00AA00530503}"; Pointer pointerToITaskSchedObj= null; ole32.CoCreateInstance(CLSID_CTaskScheduler, pUnkOuter, CLSCTX_INPROC_SERVER, IID_ITaskScheduler, pointerToITaskSchedObj);

System.out.println("pointer " + pointerToITaskSchedObj); // stays null

But the pointer stays null.

The "C++" code that Microsoft published looks like this:

int main(int argc, char **argv) { HRESULT hr = S_OK; ITaskScheduler *pITS;

hr = CoInitialize(NULL); if (SUCCEEDED(hr)) { hr = CoCreateInstance(CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER, IID_ITaskScheduler, (void **) &pITS); ..... } ..... }

My question is basically, how do I know what types to use? For instance, when I'm creating the interface, I get HRESULT, STDAPI, etc from the MSDN site, but how do I know what types to use?

Basically, if doing this thing with OLE is "in the fairway" for JNA (ie it should be easy to accomplish), then I will keep trying, but if JNA is not really "supposed" to do this kind of thing, or it would be moderately hard for someone who knew what they were doing to get it going (meaning, it would be REALLY hard for me), then I'll start looking for another way.

Thanks very much for reading through my example.

--Dale--