none
How to call a member function of a class from a dll in immediate window? C++ RRS feed

  • Question

  • Assume I have a dll (mydll.dll), which defines and exports a class "Foo" with one member function "bar()".

    I also have an exe, which dynamically links to this dll importing the class Foo with its member function. It creates a variable foo of class Foo in its main() function (and some other stuff).

    Now I put a break point right after the line where foo is created (so in my exe). And now I want to call "foo.bar()" in the immediate window to see what it would return.

    Is there a way to do that? If I just type "foo.bar()", it says "Function Foo:bar() has no address, possibly due to compiler optimizations."





    • Edited by Gertio Wednesday, June 24, 2020 4:36 PM
    Monday, June 22, 2020 7:33 PM

All replies

  • Hi Gertio,

    You could have a try this code in immediate window:

    ? foo = new Foo();
    {ClassLibrary1.Foo}
    ? foo.bar()

    More usage commands: https://stackoverflow.com/a/1361136/11557747

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Tuesday, June 23, 2020 3:25 AM
  • This is the case for C# right? Sorry I was looking for the case of C++. It doesn't seem to work there.
    Tuesday, June 23, 2020 5:37 AM
  • Hi Gertio,

    I tried C++ on my side, the error could be reproduced on my side. 

    After comparing it in C# and C++, I find the difference between them. Please refer this:

    It seems to be caused by how instantiated objects are stored. As far as I know, the C++ instantiated object is a pointer and the C# instantiated is a reference in the stack. And according to the comparison result, immediate window seems to recognize only the reference. I'm afraid that it is by designed.

    If there is something wrong, please correct me.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Tuesday, June 23, 2020 10:17 AM
  • Instantiate the unmanaged C++ object on the stack instead of the heap.

    ?foo.bar() in the immediate window will still produce the same error.

    Tuesday, June 23, 2020 10:20 AM
  • note that immediate windows does have a syntax to call a function from dll:

    {,,mydll.dll}function()

    it only works for global functions though, not member functions of classes, so this does not answer my initial question.


    Wednesday, June 24, 2020 6:06 AM
  • seems like you instantiated foo as Pointer to Foo

    Foo* foo = new Foo();

    It is also possibleto instatiate foo as Foo

    Foo foo;
    this is actually what I did in my initial question...

    Wednesday, June 24, 2020 9:23 AM
  • Looks like this:

    mydll.dll!Foo::bar()

    should work, because this is what the callstack shows, but just like this it doesn't work, it says:

    "a nonstatic member reference must be relative to a specific object",

    so it needs the `foo` object I guess, any Idea how to pass it?

    Tuesday, June 30, 2020 11:10 AM