Krazibit312
Krazibit312

Reputation: 390

DLL function gets wrong value from parameter

I have a problem with a dll function call. The function call take a struct as parameter and the struct contains a double among other data types. After call to the function the value of the double in the struct the function gets is totally different from what it was before passing to the function, something like -2.343443e4535.

here's a snippet of the call, all the dll function does for now is just print the value of the double in the struct (second parameter).

TRD_ADD myFunc= (ADD) GetProcAddress(hinstLib,"MyFunc");
Mystruct * trd = new Mystruct(1,11.1,0,0,0,0,134000);
(myFunc) (trd);

Here's the struct and function

#pragma pack(1)
struct MyStruct
 {
   int               orderNum;                     
   int               id;                   
   char              symbol[12];                 
   int               cmd;     
   char              comment[32];               
   int               internal_id;                
   int               activation;                 
   int               count;                      
   double            rate;               
   time_t            timestamp;                  
   int               reserved[4];  
   double            price;

    Mystruct(_orderNum,_rate,_timestamp,_activation,_cmd,_id,_price):orderNum(_orderNum),rate(_rate),timestamp(_timestamp),activation(_activation),cmd(_cmd),id(_id),price(_price){}
};
#pragma (pop) 
void APIENTRY MyFunc(MyStruct *myStruct)
{
   std::cout << myStruct->rate;
};

I forgot to include those #pragma's . The code is from a restricted API that's why I cannot publish the actual code but this exactly the same structure.

Thanks

Upvotes: 3

Views: 1588

Answers (1)

David Heffernan
David Heffernan

Reputation: 612954

The only plausible explanation for what you report is that the DLL has a different definition of the struct. And so when the calling code writes to members of the struct, it writes to different offsets from the offsets used in the DLL.

In the statement above, I mean also to cover the possibility that the layout of the struct is different in the DLL from that in the calling code. As it happens, for 32 bit targets, the packed and aligned layouts of your struct are the same. For 64 bit targets, packed and aligned differ. .

It's hard to say what the mismatch is from the code that you have posted. Unfortunately you seem to be a little shy. If you could only publish the entire code, for both the DLL and the code that consumes the DLL, it would be easy to tell you what the mismatch is. So, as it stands I'm afraid you'll have to work out the rest of the details, unless you can manage to publish complete code.

Upvotes: 4

Related Questions