From: Ramesh on
Hello I used module definition file to export a variable(a special
struct variable). I then tried to access it in my code, I can access
it. But the problem is, I change that external variable inside a
function. But even after changing it, it has the same value. The
following demonstration code might explain

----------------------------------------------------------------------------------------------------
//some.h
typedef struct{
double i;
double r;
}dcomplex;

dcomplex add(dcomplex a,dcomplex b);
dcomplex subtract(dcomplex a, dcomplex b);

extern dcomplex IValue;

----------------------------------------------------------------------------------------------------

//some.c #these are the exported functions
#include "some.h"
dcomplex IValue ; // this is defined as extern

dcomplex add(dcomplex a, dcomplex b){

dcomplex c;
IValue.i = 1.0;
IValue.r = 0.0;
c.i = a.i + b.i;
c.r = a.r + b.r;
return c;
}



dcomplex subtract(dcomplex a, dcomplex b){

dcomplex c;
IValue.i = 2;
IValue.r = 0.0;
c.i = a.i + b.i;
c.r = a.r + b.r;
return c;
}

---------------------------------------------------------------------------

//some.def
LIBRARY "ExportThisExperiment2"
EXPORTS
add
subtract
IValue

---------------------------------------------------------------------------------------------------------------------

// A different project using the dll generated by the other
// main.c
#include "some.h"
#include <stdio.h>

int main(){
dcomplex i,j,k;
i.i=23.52;
i.r = -12.5;
j.r = 2.356;
j.i = -22.52;
printf("i Value = (%lf + %lfi)\n",IValue.r,IValue.i);
k = add(i,j);
printf("(%lf + %lfi)+(%lf + %lfi)=(%lf + %lfi)
\n",i.r,i.i,j.r,j.i,k.r,k.i);
printf("i Value = (%lf + %lfi)\n",IValue.r,IValue.i);
k = subtract(i,j);
printf("(%lf + %lfi)-(%lf + %lfi)=(%lf + %lfi)
\n",i.r,i.i,j.r,j.i,k.r,k.i);
printf("i Value = (%lf + %lfi)\n",IValue.r,IValue.i);

return 0;
}

---------------------------------------------------------------------------

Even this simple demonstration code doesn't work. It compiles and
executes. But it is never initialized or changed. It is some junk
value always even after calling add and subtract. Any help would be
appreciated.


ramesh
From: Ramesh on
Hello I found the answer(Hate it though!). The last line here explains
it.

http://msdn.microsoft.com/en-us/library/hyx1zcd3(v=VS.80).aspx


"""Note that when you export a variable from a DLL with a .def file,
you do not need to specify __declspec(dllexport) on the variable.
However, in any file that uses the DLL, you must still use
__declspec(dllimport) on the declaration of data."""

Thanks

On May 18, 2:26 pm, Ramesh <ramesh10dul...(a)gmail.com> wrote:
> Hello I used module definition file to export a variable(a special
> struct variable). I then tried to access it in my code, I can access
> it. But the problem is, I change that external variable inside a
> function. But even after changing it, it has the same value. The
> following demonstration code might explain
>
> --------------------------------------------------------------------------- -------------------------
> //some.h
> typedef struct{
>         double i;
>         double r;
>
> }dcomplex;
>
> dcomplex add(dcomplex a,dcomplex b);
> dcomplex subtract(dcomplex a, dcomplex b);
>
> extern dcomplex IValue;
>
> --------------------------------------------------------------------------- -------------------------
>
> //some.c #these are the exported functions
> #include "some.h"
> dcomplex IValue ; // this is defined as extern
>
> dcomplex add(dcomplex a, dcomplex b){
>
>         dcomplex c;
>         IValue.i = 1.0;
>         IValue.r = 0.0;
>         c.i = a.i + b.i;
>         c.r = a.r + b.r;
>         return c;
>
> }
>
> dcomplex subtract(dcomplex a, dcomplex b){
>
>         dcomplex c;
>         IValue.i = 2;
>         IValue.r = 0.0;
>         c.i = a.i + b.i;
>         c.r = a.r + b.r;
>         return c;
>
> }
>
> ---------------------------------------------------------------------------
>
> //some.def
> LIBRARY "ExportThisExperiment2"
> EXPORTS
>         add
>         subtract
>         IValue
>
> --------------------------------------------------------------------------- ------------------------------------------
>
> // A different project using the dll generated by the other
> // main.c
> #include "some.h"
> #include <stdio.h>
>
> int main(){
>         dcomplex i,j,k;
>         i.i=23.52;
>         i.r = -12.5;
>         j.r = 2.356;
>         j.i = -22.52;
>         printf("i Value = (%lf + %lfi)\n",IValue.r,IValue.i);
>         k = add(i,j);
>         printf("(%lf + %lfi)+(%lf + %lfi)=(%lf + %lfi)
> \n",i.r,i.i,j.r,j.i,k.r,k.i);
>     printf("i Value = (%lf + %lfi)\n",IValue.r,IValue.i);
>         k = subtract(i,j);
>         printf("(%lf + %lfi)-(%lf + %lfi)=(%lf + %lfi)
> \n",i.r,i.i,j.r,j.i,k.r,k.i);
>         printf("i Value = (%lf + %lfi)\n",IValue.r,IValue.i);
>
>         return 0;
>
> }
>
> ---------------------------------------------------------------------------
>
> Even this simple demonstration code doesn't work. It compiles and
> executes. But it is never initialized or changed. It is some junk
> value always even after calling add and subtract. Any help would be
> appreciated.
>
> ramesh

From: Ulrich Eckhardt on
Ramesh wrote:
> Hello I found the answer(Hate it though!). The last line here explains
> it.
>
> http://msdn.microsoft.com/en-us/library/hyx1zcd3(v=VS.80).aspx
>
>
> """Note that when you export a variable from a DLL with a .def file,
> you do not need to specify __declspec(dllexport) on the variable.
> However, in any file that uses the DLL, you must still use
> __declspec(dllimport) on the declaration of data."""

Why do you want to use a .def file anyway? The only reason I have used it is
to define the exact names so you can easier load the DLL using LoadLibrary.
Otherwise, you get some decorations according to the calling convention in
the exported symbols. However, generally you want that additional
safeguard, so that you at least get linker errors when you are trying to
use the DLL with the wrong calling conventions.

Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932