Re: HELP!!! swt + mozilla (with more questions)

jml <[email protected]> Wed, 18 Dec 2002 12:04:51 +0800
Newsgroups gmane.comp.mozilla.devel.java,gmane.comp.mozilla.devel.embedding,gmane.comp.mozilla.devel.xpcom
Organization Another Netscape Collabra Server User
Message-ID <[email protected]>
Hi, Doug and others,
Thanks very much for the reply, now I can get 
nsIComponentRegistrar.enumerateContractIDs to work. This is my first 
milestone that I can get something to work using swt+mozilla :-)

Let me explain my approach.

In swt for win32, there has a lot of predefined native methods calls, 
the most useful one to me is (there is a set of methods like this in 
swt, with different parameter types):
COM.VtblCall( fnNumber, ppVtbl, arg0, arg1, ...... )

The swt include native implementation for these Java methods.

Then in my java code, I can do things as:

public nsISimpleEnumerator enumerateContractIDs() {
	int result[] = new int[1];
	XPCOM.checkException(COM.VtblCall(12, address, result));
	return new nsISimpleEnumerator(result[0]);
}

(XPCOM is my helper class)

This are all Java methods, without going to the native code. (I am more 
comfortable with java code than C/C++, so C/C++ guys please forgive me :-)

Currently, I only have a very small set of my own native code, including 
loading the xpcom.dll file, and calling the NS_InitXPCOM2, 
NS_GetComponentManager, etc. I try to limit my dependency on the Mozilla 
system only to the xpcom.dll file. That is, I do not want include 
Mozilla include files, I do not want to link with mozilla libraries, 
etc. In my native code, I just call LoadLibaray("xpcom") and 
GetProcAddress()


My first runnable test main file look as: (setting the work directory to 
c:\mozilla)

public static void main(String[] args) {
	Embed.NS_InitEmbedding(0,0);
	nsIComponentRegistrar registrar = Embed.getComponentRegistrar();
	nsISimpleEnumerator enum = registrar.enumerateContractIDs();
	int count = 0;
	while( enum.hasMoreElements()) {
		nsISupports s = enum.getNext();
		nsISupportsCString cstring = 
(nsISupportsCString)s.queryInterface(nsISupportsCString.class);
		s.Release();
		System.out.println(cstring.toString());
		cstring.Release();
	}
	Embed.NS_TermEmbedding();
}

The output look as follows:

@mozilla.org/persistent-properties;1
@mozilla.org/file/local;1
@mozilla.org/supports-PRUint64;1
......
@mozilla.org/supports-void;1
@mozilla.org/consoleservice;1
@mozilla.org/arena;1
com.voxa.swt.xpcom.XPCOMException: XPCOMException, code=-2147467259
	at com.voxa.swt.xpcom.XPCOM.checkException(XPCOM.java:98)
	at 
com.voxa.swt.xpcom.nsISimpleEnumerator.getNext(nsISimpleEnumerator.java:66)
	at com.voxa.swt.xpcom.test.TestMain.main(TestMain.java:28)
Exception in thread "main"

I still don't know why I get an exception, I think after 
hasMoreElements() return true, should not have this exception any more. 
Maybe my hasMoreElements() implementation has some problem, I need to look.


Then the following are my further questions:

1. As in the printed out contrat IDs, I didn't find 
"@mozilla.org/embedding/browser/nsWebBrowser;1", I think it means this 
component is not registered. What should I do to make it to be 
registered? (same as the fifth question in previous post)

2. For the nsISupportsCString.toString() method, I receive a "string" 
pointer, do I need to call any method to release the memory associated 
with it? If yes, which method should I call? 
NS_GetMemoryManager.free(ptr)? I tried this, but seemed it do not return 
NS_OK.

3. Currently I am hand coding those nsI... classes in Java, which is a 
tedious work, and easy for making error, especially those fnNumbers. Is 
there any easy way to extend XPIDL to generate Java code?

4. For AString and ACString, when I receive these from XPCOM calls, as I 
said, I am only using xpcom.dll file, so there is no nsAString.h file 
included in my native code, in such as situation, how can I manipulate 
it? Can I also manipulate it using the vtbl? If I can, what is the 
binary vtbl format for it?


BTW, to answer my own first question in previous post, it seemed that 
XPCOM is binary compatible with COM/OLE vtable on win32.


Thanks in advance.

yang


Doug Turner wrote:
> jml wrote:
> 
>> 1. Is XPCOM binary compatible with COM/OLE?
>> In swt, it use vtbl to call COM/OLE methods. I want to just use that 
>> provided by swt, then I needn't write my native code.
> 
> 
> XPCOM uses vtbls as well as its binary format.  How compatiblity XPCOM 
> is wrt COM/OLE, i am not sure.
> 
> What native code would you have to write if XPCOM wasn't compatible with 
> COM/OLE.  I am not sure I completely follow.
> 
>> 2. what is the "string"/"wstring"/"domstring" mean in XPCOM?
>> What is the binary format of each of them? Since I need to package the 
>> java String into a memory buffer then pass the pointer of the buffer 
>> to XPCOM methods, so I need to know the format for this buffer.
> 
> 
> These types map to the AString and ACString.  Take a look at the 
> nsEmbedString for a very light implementation of this virtual class.
> 
> http://lxr.mozilla.org/seamonkey/source/xpcom/glue/standalone/nsEmbedString.cpp#40 
> 
> 
>> 3. Is there anything similiar to IDispatch? How I do dynamically get 
>> property and call methods?
> 
> 
> Nothing for public comsumption.  Note that there has been some work done 
> to allow communication with COM objects that support IDispatch 
> interfaces.  However, there is nothing in XPCOM that directly supports a 
>  IDispatch like interface.
> 
>> 4. How does nsIComponentRegistrar.enumeratoeContractID work?
>> What will the nsISimpleEnumerator.getNext() return then? It seemed in 
>> mozilla, the documentation is too little.
> 
> 
> I just posted a response to this in the .xpcom newsgroup.  (The contract 
> id enumeration can be QI'ed for nsISupportsCString.  The cid enumeration 
> can be QI'ed for a nsISupportsID.)
> 
>> 5. How I make the web

  component to be registered?
> 
> 
> Registered by what or how?
> 
> Have you taking a look a the embedding documentation.  It explains many 
> parts of the Gecko system which will aide you in embedding Gecko into swt.
> 
> http://www.mozilla.org/projects/embedding/
> 
> Regards,
> 
> Doug Turner
> [email protected]
>