Thread: Passing a pointer to a struct into a dll

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    2

    Passing a pointer to a struct into a dll

    windows 10
    visual studio 2019 community

    I have an exe project with a large struct in it. I also have a dll project. I pass a pointer to the struct in the exe into the dll after loading the dll with LoadLibrary.

    If I inspect the struct whilst debugging the exe it is as it should be. If I call a function in the dll I can see (in the debugger) that immediately upon calling the function the 2nd half of the struct is corrupted (the data changes). Immediately upon the dll function returning to the exe I can see (in the debugger) that the corrupted data returns to what it should be.

    It's my understanding that the exe and dll share the same address space, so why is the data in the struct corrupted when inside the dll and why does it return to normal when returning to the dll? Can I mark the struct in some way to make it stay valid when inside the dll? All I can think of is to copy the struct into the dll instead of passing a pointer but I'd rather not because the data in the struct changes over time.

    In exe:

    Code:
    struct FOO
    {
    ...
    };
    FOO foo =
    {
    ...
    };

    In dll:


    Code:
    FOO* dllFoo = NULL;

    dllFoo is exported in a def file, it's address is obtained using GetProceAddress, and a pointer to foo is copied into it. All this must be ok because the first half of the struct is correct when inside the dll (it's complex data not all zeros, and it's correct up to a point).

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I have an exe project with a large struct in it.
    What constitutes 'large'?
    1KB
    4KB
    64KB
    1MB
    ?

    What happens in the DLL if it dereferences the pointer to supposedly read a garbage value, does it in fact read garbage?
    In other words, is it wrong in the code as well, or is it just a failure in the debugger?

    Cross-posted here as well -> Passing a pointer to a struct into a dll - C++ Forum
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2021
    Posts
    2
    Quote Originally Posted by Salem View Post
    > What constitutes 'large'?
    About 1KB (of data and some function pointers).

    Quote Originally Posted by Salem View Post
    What happens in the DLL if it dereferences the pointer to supposedly read a garbage value, does it in fact read garbage?
    In other words, is it wrong in the code as well, or is it just a failure in the debugger?
    It reads garbage - that's how I noticed it to begin with, because the garbage value caused the program to crash.

    In the debugger, when inside the exe, the debugger displays the data correctly. Immediately upon the call instruction to enter the dll the 2nd half of the data becomes garbage. Then, immediately upon the return instruction to the exe, the 2nd half of the data becomes correct again. I've been programming for a long while and I can't understand it at all. I've never seen something like this before. I could understand it if the data got corrupted and stayed corrupted, but for it to go back to a good state makes no sense to me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing struct pointer to function
    By Nikosant03 in forum C Programming
    Replies: 7
    Last Post: 04-22-2020, 03:21 AM
  2. passing struct pointer to function.
    By Durango2011 in forum C Programming
    Replies: 3
    Last Post: 10-28-2011, 07:10 PM
  3. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  4. Passing Pointer-to-struct to a function!!
    By Lau in forum C Programming
    Replies: 5
    Last Post: 11-18-2002, 12:59 PM
  5. Passing a pointer to a struct
    By Natase in forum C Programming
    Replies: 2
    Last Post: 10-02-2001, 10:29 AM

Tags for this Thread