locked
DLL return error and solution RRS feed

  • Question

  • I have used C++ express edition to program a hid usb dll. When I compile it and call funtion in JAVA, it returned true.

    bool MyDeviceStatus = false;

    extern "C" __declspec(dllexport) bool __cdecl GetMyDeviceStatus()
    {
    return MyDeviceStatus;
    }

    Maybe the problem is a memory access.. I changed codes like below but this is not the solution. 

    extern "C" __declspec(dllexport) bool __cdecl GetMyDeviceStatus()
    {
    return ::MyDeviceStatus;
    }

    Is it not a scope with memory error.. I found.. 

    extern "C" __declspec(dllexport) bool __cdecl GetMyDeviceStatus()
    {

    bool status = new bool();

    status=MyDeviceStatus;

    return status;

    }

    this is solution.. if i use 'if' the problem solved... if you want compile release mode you have disable optimization (/Od)

    extern "C" __declspec(dllexport) bool __cdecl GetMyDeviceStatus()
    {

    bool status = new bool();
    if (MyDeviceStatus)
    status=true;
    else
    status=false;

    return status;
    }

    This is a compiler error.. Please solve it, microsoft.. 

    Tuesday, February 7, 2012 2:55 PM

Answers

  • The thing is, when I do

    bool status = false;
    
    extern"C"
    __declspec(dllexport)
    bool getstatus()
    {
    	return status;
    }

    from a DLL and call it in a C++ application, it returns false. Even with the optimizer on. That is why I was asking if you had tried it with a C/C++ application.

    As to allocation. Do you know what the new operator is for? From the MSDN documentation

    Allocates memory for an object or array of objects of type-name from the free store and returns a suitably typed, nonzero pointer to the object.

    so you did allocate memory in the 3rd and 4th functions above by using new.

    Oh, and for bool status = NULL;, this is no better than bool status = false;. This is worse because it makes your code unclear because it has to do an integer to boolean conversion.


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.


    • Edited by Darran Rowe Wednesday, February 8, 2012 6:24 PM
    • Proposed as answer by Jesse Jiang Tuesday, February 14, 2012 5:57 AM
    • Marked as answer by Jesse Jiang Wednesday, February 22, 2012 2:33 AM
    Wednesday, February 8, 2012 6:18 PM

All replies

  • i want to emphasize to be created usign bool status = new bool(); in 3th codes..

    bool status=NULL; this is a goodier choice

    Tuesday, February 7, 2012 3:04 PM
  • Actually that is a bad choice.

    Since status is not a pointer type, you aren't allocating memory to it. What you are actually doing is allocating memory, assigning the value that it contains to status and then it leaks the memory that it just allocated.

    Did you try calling these DLL functions from a C/C++ application? Because quite simply, first one is all you need. Unless something else changes the value at runtime then it should be returning false. Try using it in a C/C++ application and try putting a data breakpoint on the variable itself. It will break into your application when that variable is modified so you know whether something else has interfered.


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Tuesday, February 7, 2012 3:15 PM
  • status is a bool variable and it defined compiler time. and its a native code.. any programming language don't care, i think.. yes runtime some function using  MyDeviceStatus if HID USB connected or not.. and change the value.. but in the first time, when any function haven't called, you have to obtain compiling time value(false). but if you haven't use the last code(it's in below).. it will returned true.. so, it means compiler time it isn't defined as false by compiler to dll file or the dll code couldn't attain it in memory as false for first time.. first ,second or third function codes can't compile as correct in spite of simplicity.. i haven't try any allocating here.. it's out of subject..  misunderstanding here, i think.. :)

    I mean.. if you define a compiler time bool value.. and if you want to obtain it, you have to process it in vain.. seperately you can't use optimization.. okey.. i know sometimes you can't use optimization.. it will change opcodes flow.. but it is very simple code.. only a compile time value and a return.. :D

    bool MyDeviceStatus = false;

    extern "C" __declspec(dllexport) bool __cdecl GetMyDeviceStatus()
    {

    bool status=NULL;

    if (MyDeviceStatus)
    status=true;
    else
    status=false;

    return status;
    }

    Tuesday, February 7, 2012 10:20 PM
  • Is it correct to use __cdecl in declaration? According to JDK you should use __stdcall or JNICALL.




    • Edited by Viorel_MVP Wednesday, February 8, 2012 7:02 AM
    Wednesday, February 8, 2012 7:01 AM
  • yes __cdecl is correct use..(because of JNA) you can find some information on net as if it is false.. but 

    <tt></tt>

    __stdcall function names like this "_Function@n" and __cdecl function names like this "Function".. you cannot use @ character in a function name in JAVA.. so i have written a USB HID library with __cdecl and using in JAVA.. any problem i have met.. 



    <tt></tt>


    • Edited by Ali Tatlı Wednesday, February 8, 2012 4:34 PM
    Wednesday, February 8, 2012 10:35 AM
  • The thing is, when I do

    bool status = false;
    
    extern"C"
    __declspec(dllexport)
    bool getstatus()
    {
    	return status;
    }

    from a DLL and call it in a C++ application, it returns false. Even with the optimizer on. That is why I was asking if you had tried it with a C/C++ application.

    As to allocation. Do you know what the new operator is for? From the MSDN documentation

    Allocates memory for an object or array of objects of type-name from the free store and returns a suitably typed, nonzero pointer to the object.

    so you did allocate memory in the 3rd and 4th functions above by using new.

    Oh, and for bool status = NULL;, this is no better than bool status = false;. This is worse because it makes your code unclear because it has to do an integer to boolean conversion.


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.


    • Edited by Darran Rowe Wednesday, February 8, 2012 6:24 PM
    • Proposed as answer by Jesse Jiang Tuesday, February 14, 2012 5:57 AM
    • Marked as answer by Jesse Jiang Wednesday, February 22, 2012 2:33 AM
    Wednesday, February 8, 2012 6:18 PM