bool parameter problem in DLL function

Discussion in 'C++' started by k.sahici, Jun 15, 2007.

  1. k.sahici

    k.sahici Guest

    I have a DLL written in C++ which has a function shown below.

    /**********************************
    //MYDLL.cpp

    __declspec(dllexport)
    void Pos(MyStruc &r)
    {
    r.var = true;
    }
    /**********************************

    and I export it in Export.def
    /**********************************
    LIBRARY MYDLL
    EXPORTS
    Pos
    /**********************************

    MyStruc is also declared as
    /**********************************
    Struc MyStruc{
    bool var;
    MyStruc( ){
    var = false;
    };
    /**********************************
    As you see above, MyStruc has a default constructor which sets var to
    false.

    When I use this dll in another C++ source file as

    /**********************************
    HINSTANCE hGetProcIDDLL = LoadLibrary("MYDLL.dll");
    FARPROC lpfnGetProcessID = GetProcAddress(HMODULE(hGetProcIDDLL),
    "Pos");

    if (lpfnGetProcessID == NULL)
    {
    return;
    }
    typedef void(*pICFUNC) (lpfnGetProcessID);

    pICFUNC pfunc = pICFUNC(lpfnGetProcessID);

    MyStruc r;

    pfunc(r);
    /**********************************
    This code(in other words, Pos function in MYDLL.dll) is supposed to
    change r.var to true, but somehow it doesn't and when I check its
    value I see that it's still false. When I change the default
    constructor to set var to true and change the Pos function to set
    r.var to false, I see that var is still true.

    To sum up, the default constructor overwrites what my Pos(MyStruc &r )
    does. The interesting thing is, I write the DLL and the program which
    uses this DLL in the same programming language and with the same IDE,
    Visual C++ 6.0. I should also point out that I change none of the
    project settings. I think it is mainly about using bool type in a DLL.

    Do you have any idea about this weird behavior?
    k.sahici, Jun 15, 2007
    #1
    1. Advertising

  2. k.sahici

    dasjotre Guest

    On 15 Jun, 15:07, "k.sahici" <> wrote:
    > __declspec(dllexport)
    > void Pos(MyStruc &r)
    > {
    > r.var = true;}
    >


    > HINSTANCE hGetProcIDDLL = LoadLibrary("MYDLL.dll");
    > FARPROC lpfnGetProcessID = GetProcAddress(HMODULE(hGetProcIDDLL),
    > "Pos");


    i think that HMODULE is just an alias
    for HINSTANCE, no need to cast

    >
    > if (lpfnGetProcessID == NULL)
    > {
    > return;}
    >
    > typedef void(*pICFUNC) (lpfnGetProcessID);


    so pICFUNC is a pointer to a function returning void
    with one argument of type FARPROC. like:

    void weird_fun(FARPROC);

    > pICFUNC pfunc = pICFUNC(lpfnGetProcessID);


    typedef void (*pICFUNC)(MyStruc &);
    pICFUNC pfunc = (pICFUNC)(lpfnGetProcessID);

    > MyStruc r;
    >
    > pfunc(r);


    I don't believe this should compile in the first place.
    you can't convert MyStruc to FARPROC

    > Do you have any idea about this weird behavior?


    it is called 'undefined behaviour'

    regards

    DS
    dasjotre, Jun 15, 2007
    #2
    1. Advertising

  3. k.sahici

    dasjotre Guest

    On 15 Jun, 16:06, dasjotre <> wrote:
    > > typedef void(*pICFUNC) (lpfnGetProcessID);


    ups almost missed this one!
    surely that won't compile, lpfnGetProcessID is not a type

    VC6 is very old and not even supported by MS any more.
    you can download free community edition of VC8 from
    MS. also, using lib files in conjunction with DLLs makes
    it much easier than LoadLibrary/GetProcAddress and
    you can't make the kind of the errors you made.

    regards

    DS
    dasjotre, Jun 15, 2007
    #3
  4. k.sahici

    Glen Dayton Guest

    k.sahici wrote:
    > I have a DLL written in C++ which has a function shown below.
    >
    > /**********************************
    > //MYDLL.cpp
    >
    > __declspec(dllexport)
    > void Pos(MyStruc &r)
    > {
    > r.var = true;
    > }
    > /**********************************
    >
    > and I export it in Export.def
    > /**********************************
    > LIBRARY MYDLL
    > EXPORTS
    > Pos
    > /**********************************
    >
    > MyStruc is also declared as
    > /**********************************
    > Struc MyStruc{
    > bool var;
    > MyStruc( ){
    > var = false;
    > };
    > /**********************************
    > As you see above, MyStruc has a default constructor which sets var to
    > false.
    >
    > When I use this dll in another C++ source file as
    >
    > /**********************************
    > HINSTANCE hGetProcIDDLL = LoadLibrary("MYDLL.dll");
    > FARPROC lpfnGetProcessID = GetProcAddress(HMODULE(hGetProcIDDLL),
    > "Pos");
    >
    > if (lpfnGetProcessID == NULL)
    > {
    > return;
    > }
    > typedef void(*pICFUNC) (lpfnGetProcessID);
    >
    > pICFUNC pfunc = pICFUNC(lpfnGetProcessID);
    >
    > MyStruc r;
    >
    > pfunc(r);
    > /**********************************
    > This code(in other words, Pos function in MYDLL.dll) is supposed to
    > change r.var to true, but somehow it doesn't and when I check its
    > value I see that it's still false. When I change the default
    > constructor to set var to true and change the Pos function to set
    > r.var to false, I see that var is still true.
    >
    > To sum up, the default constructor overwrites what my Pos(MyStruc &r )
    > does. The interesting thing is, I write the DLL and the program which
    > uses this DLL in the same programming language and with the same IDE,
    > Visual C++ 6.0. I should also point out that I change none of the
    > project settings. I think it is mainly about using bool type in a DLL.
    >
    > Do you have any idea about this weird behavior?
    >


    This is really a C++ newsgroup, but maybe someday C++ will have
    standard features for shareable and dynamic link libraries.

    Check that you're also exporting the entire MyStruc class. If
    you're merely defining it in a header the Microsoft DLL and the
    application that loads the DLL will have their own copies rather
    than a common copy.
    Glen Dayton, Jun 15, 2007
    #4
  5. k.sahici

    k.sahici Guest

    On Jun 15, 6:16 pm, dasjotre <> wrote:
    > On 15 Jun, 16:06, dasjotre <> wrote:
    >
    > > > typedef void(*pICFUNC) (lpfnGetProcessID);

    >
    > ups almost missed this one!
    > surely that won't compile, lpfnGetProcessID is not a type
    >
    > VC6 is very old and not even supported by MS any more.
    > you can download free community edition of VC8 from
    > MS. also, using lib files in conjunction with DLLs makes
    > it much easier than LoadLibrary/GetProcAddress and
    > you can't make the kind of the errors you made.
    >
    > regards
    >
    > DS


    First of all, I'd like to thank all of you for your replies.

    I've just realized that I've mistyped my question.
    typedef void(*pICFUNC) (lpfnGetProcessID); //I know this is wrong and
    actually I wrote
    typedef void(*pICFUNC) (MyStruc &); in my source file

    However, last time I checked it, I saw it was working. But I'm sure it
    was not working when I posted this question a few days ago. Is it
    possible that there is undefined behavior about using bool parameter
    type in a dll function?
    k.sahici, Jun 18, 2007
    #5
  6. k.sahici

    dasjotre Guest

    On 18 Jun, 07:52, "k.sahici" <> wrote:
    > On Jun 15, 6:16 pm, dasjotre <> wrote:
    >
    >
    >
    > > On 15 Jun, 16:06, dasjotre <> wrote:

    >
    > > > > typedef void(*pICFUNC) (lpfnGetProcessID);

    >
    > > ups almost missed this one!
    > > surely that won't compile, lpfnGetProcessID is not a type

    >
    > > VC6 is very old and not even supported by MS any more.
    > > you can download free community edition of VC8 from
    > > MS. also, using lib files in conjunction with DLLs makes
    > > it much easier than LoadLibrary/GetProcAddress and
    > > you can't make the kind of the errors you made.

    >
    > > regards

    >
    > > DS

    >
    > First of all, I'd like to thank all of you for your replies.
    >
    > I've just realized that I've mistyped my question.
    > typedef void(*pICFUNC) (lpfnGetProcessID); //I know this is wrong and
    > actually I wrote
    > typedef void(*pICFUNC) (MyStruc &); in my source file
    >
    > However, last time I checked it, I saw it was working. But I'm sure it
    > was not working when I posted this question a few days ago. Is it
    > possible that there is undefined behavior about using bool parameter
    > type in a dll function?


    It is possible that there is a bug in your compiler causing this. My
    experience is that whenever I try to blame the compiler it turns out
    that the mistake was mine :)

    regards.

    DS
    dasjotre, Jun 18, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anushi
    Replies:
    5
    Views:
    8,637
  2. andy
    Replies:
    4
    Views:
    756
    Jonathan Turkanis
    Feb 21, 2004
  3. Baohua Li

    Pass bool parameter to web method

    Baohua Li, Jan 16, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    432
    Jan Tielens
    Jan 20, 2004
  4. H. Simpson
    Replies:
    4
    Views:
    247
    H. Simpson
    Aug 3, 2004
  5. AzamSharp
    Replies:
    2
    Views:
    162
Loading...

Share This Page