How to retrieve a char pointer from a DLL
Volker Voßkämper <[email protected]>
| Newsgroups | gmane.comp.windows.devel.jawin |
|---|---|
| Message-ID | <LISTSERV%[email protected]> |
Hello everybody,
I'm new to jawin and I'm really not a C programmer. I try to use a DLL
for some measurement boards. Some funktions that return nummeric values
work well but I can't figure out how to get a string from a char pointer.
This is the code I tried:
----------------------
(...)
private static final int BOARDNAMELEN = 25; // as defined in cbw32.h
private static FuncPtr cbGetBoardNamePtr = null;
public static int cbGetBoardName(int boardNum, StringBuffer boardName){
// DLL Signature: int cbGetBoardName(int BoardNum, char *BoardName)
int returnVal=9999;
try {
cbGetBoardNamePtr = new FuncPtr("CBW32.DLL", "cbGetBoardName");
NakedByteStream nbs = new NakedByteStream();
LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
leos.writeInt(boardNum);
byte[] res = cbGetBoardNamePtr.invoke("IA:I:L4n" + BOARDNAMELEN, 8,
nbs, null, ReturnFlags.CHECK_W32);
ByteArrayInputStream bais = new ByteArrayInputStream(res);
LittleEndianInputStream leis = new LittleEndianInputStream(bais);
returnVal = leis.readInt();
//boardName = leis.readStringUnicode(); // this give an error !!
// so tried:
boardName.delete(0,boardName.length()); // delete possible content of
the buffer
String byteStr;
try {
do {
byteStr = String.valueOf(leis.readChar());
if ( byteStr.equals("0") ) {
break;
}
boardName.append(byteStr);
} while ( ! byteStr.equals("0") );
} catch (Exception e) { }
System.out.println("Boardname Bytes: " + boardName.toString());
} catch (COMException e) {
e.printStackTrace();
returnVal=9999;
} catch (IOException ie) {
ie.printStackTrace();
returnVal=9999;
} finally {
if (cbGetBoardNamePtr != null) {
try {
cbGetBoardNamePtr.close();
} catch (COMException e) {
e.printStackTrace();
}
}
}
return returnVal;
}
(...)
-----------------
the output is:
at de.cenote.mcc.lib.Cbw32.cbGetBoardName(Cbw32.java:259)
GenericStub.win32Invoke
instructions: IA:I:L4n25
stackSize: 8
flags: [CHECK_W32 (3)]
argStream: 32, argStreamSize: 4
02 00 00 00 00 00 00 00 ........
00 00 00 00 00 00 00 00 ........
00 00 00 00 00 00 00 00 ........
00 00 00 00 00 00 00 00 ........
GenericStub.comInvoke result
00 00 00 00 6D 69 6E 69 ....mini
4C 41 42 20 31 30 30 38 LAB 1008
00 00 00 00 03 00 03 00 ........
0B 01 08 00 20 ....
Boardname Bytes: ??????
---------------
"mini LAB 1008" is the string I need....
hope someone could help...
Thanks
Volker