LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Passing 32-bit Int value to C++ dll

Hi,
 
I have been struggling with this simple problem for quite a few days now.  I developed a C++ dll to be called from Labview. I use call library function in labview. I can call functions which do not require any inputs. However, if I call a function with simple int input, labview crashes. I have looked into so many examples on Labview and C/C++ dll and followed their solutions but no success. What am I doing wrong? Any help is appreciated.
 
Thanks.
 
C++ Function Code:

/********************************************************************************************

/* Name: Write32BitWord

/* Purpose: This function writes a 32-bit word into the volatile memory of the connected target

/* Input: _32BitData: 32-bit value to write

/* address: address where to write 32-bit word

/* Output: none

/* Return: error code

/********************************************************************************************/

extern "C" int AT91SAM7S::Write32BitWord(int _32BitData, int address)

{

IAT91BootDLLPtr pAT91BootDLL(

__uuidof(AT91BootDLL));

int errCode;

pAT91BootDLL->AT91Boot_Write_Int(hHandle, _32BitData, address, &errCode);   //hHandle is global variable

return (errCode);

}

Labview Code:

See attachments

0 Kudos
Message 1 of 5
(3,240 Views)
I tried to attach labview vi but for some reason the post doesnt show it.
0 Kudos
Message 2 of 5
(3,221 Views)
Did you change to calling convention from "C" to "stdcall (WINAPI)"? You
should.

Regards,

Wiebe.


0 Kudos
Message 3 of 5
(3,194 Views)
Yes, it worked. Thanks a lot. But why is that functions that do not pass any argument work with C convention and others not?
0 Kudos
Message 4 of 5
(3,182 Views)
All parameters are pushed on the processors stack. They must be popped from
the stack in the correct order. In one convention, the return address is
pushed after all parameters, the other pushes it before all parameters. So
the stack for the call is exactly the same for both, only reversed.

So if you call a function with a number, what is pushed onto the stack is:

number, return address. (stdcall)
or
return address, number. (C)

If the convention is wrong, the function will use the return address as
number, and the number as return address! So it will jump to an arbitrary
memory address, where execution is resummed. This will crash the process for
sure.

If you have no parameters, the it doesn't matter if the return address is
pushed first or last, since it will end up as first and last (since there is
nothing in between):

return address.
or
return address.

And all's well...

Regards,

Wiebe.



Message 5 of 5
(3,173 Views)