Skip to main content

[solved] JNA, Windows7, Structs, DEBUG ASSERTION

1 reply [Last post]
TSRi
Offline
Joined: 2011-04-18
Points: 0

Hi there,
I have a problem in my JNA-Project. First of all, please have a look at my Code:
C++

struct  HelloStruct
{
    unsigned long something;
    wchar_t* terminal_str;                 
};
typedef void (*callfunc)(HelloStruct*);
void __cdecl phon_callFuncPointer(callfunc a  );
[...]
void __cdecl phon_callFuncPointer(callfunc a  ){
    HelloStruct s;
    s.something = 12345;
    s.terminal_str = new wchar_t[6];
    wcscpy(s.terminal_str, L"Hello");
    a(&s);
    delete[] s.terminal_str;

}

On the JAVA-side:
public interface MyInterface extends Library {
public class Intern_TestText extends Structure {
/// bearbeiteter Strem beginnt bei Byte
public NativeLong someval;
public WString text;
}
public interface _internal_HelloCallback extends Callback{
void HelloWStringCallback(Intern_TestText wstr);
}
void phon_callFuncPointer( _internal_HelloCallback a );
}
[...]
dll.phon_callFuncPointer(new PhonInterface._internal_HelloCallback() {



@Override
public void HelloWStringCallback(Intern_TestText wstr) {
System.out.println("CALLBACK: " + wstr.test);

}
});

This produce a DEBUG ASSERTION ERROR at the delete Operation in the C++ part. I tried to pass a sigle WString, that works! But when I pass a WString in a simple struct I get an error. How can I fix this problem?!
This Programm works for Windows XP perfectly, so where is my mistake of thinking?!
My System:
Windows 7 64 Bit
JAVA 32 Bit-version (I cannot use the 64 Bit version....)

THANKS for your help!
Greez

TSRi

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
TSRi
Offline
Joined: 2011-04-18
Points: 0

This seems to be a failure in JNA, but I've found a workaround:

Use Pointers instead of WString. Then you can use Function p.getString(0,true) to get an String from Unicode. Or you read all Bytes(then you need the length(in Bytes) too) and Parse the String by yourself.

C++

struct  HelloStruct
{
    long something;
    wchar_t* terminal_str;  
    long strLenInBytes
};
typedef void (*callfunc)(HelloStruct*);
void __cdecl phon_callFuncPointer(callfunc a  );
[...]
void __cdecl phon_callFuncPointer(callfunc a  ){
    HelloStruct s;
    s.something = 12345;
    s.terminal_str = new wchar_t[6];
    wcscpy(s.terminal_str, L"Hello");
    s.strLenInBytes = wcslen(L"Hello") * 2; //2 Bytes for each Unicode-Character in Windows
    a(&s);
    delete[] s.terminal_str;

<p>}</p>

Change the JAVA Site to:

public interface MyInterface extends Library {
public class Intern_TestText extends Structure {

public NativeLong someval;
public Pointer text;
                public NativeLong textLenInBytes;
}
public interface _internal_HelloCallback extends Callback{
void HelloWStringCallback(Intern_TestText wstr);
}
void phon_callFuncPointer( _internal_HelloCallback a );
}
[...]
dll.phon_callFuncPointer(new PhonInterface._internal_HelloCallback() {
                       @Override
public void HelloWStringCallback(Intern_TestText wstr) {
                                String theStr = p.getString(0, true);
                                //ALTERNATIVE:
                                /* byte[] bytes = p.getByteArray(0L, len);
        try {
    ret = new String(bytes, &quot;UTF-8&quot;);
    ret = ret.trim();
        } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
        }*/
System.out.println(&quot;CALLBACK: &quot; + wstr.test);

}
});

Greez

TSRi