Jump to content

How do I export/import variables from a DLL to an EXE?

- - - - -

  • Please log in to reply
1 reply to this topic

#1
coffeenet

coffeenet

    Newbie

  • Members
  • Pip
  • 3 posts
Hi,
Honestly, this is not specifically related to Source SDK. But, I need help, and desperate... Sorry...
I am trying to export/import global variables in a Windows Env. (Visual Studio 2010 Pro Ed)
I wrote the below code, which I think is correct. However, the value is not mirrored correctly to the EXE.
Furthermore, I am using a Game Mod, where the DLLs are located in a different folder than the EXE. So, I would like someone to tell me how to correctly type in the "Additional Dependency" option.

Last, I would like to know the correct syntax of this if I would port my code to Linux.

The code:

    //DLL side

    #define __ZG_DLLEXPORT__ __declspec(dllexport)

        __ZG_DLLEXPORT__ int xyzzyx = -1;

    void smFunc()

    {

        	if( xyzzyx < 10000 )

        	{

        		xyzzyx += 4;

        	}

    }

/-----------------/

        //Exe Side

    #define __ZG_DLLIMPORT__ __declspec(dllimport)

    

    __ZG_DLLIMPORT__ int xyzzyx;

    

    int XXXXX = 0; int IIII = 9;

    void funcX()

    {

        	XXXXX = xyzzyx;

        	if( XXXXX > 100 )

        	{

        						fp = fopen("SOMETHING.txt", "a");

        				if( fp != NULL )

        				{

        					fprintf(fp, "%d", XXXXX );

        					fprintf(fp, "\n" );

        				}

        				fclose(fp);

        

        	}

    }

/---output---/ you can ignore the Additional Dependency, because I am not familiar with the proper syntax.

    //Additional Dependency: Mod folder, which contains *.lib and *.dll files:

    //The value of xyzzyx is always -1,

    

    //Additional Dependency: Exe folder, the program crashes saying DLL is corrupt.


Edited by coffeenet, 15 December 2011 - 10:19 PM.


#2
kernelcoder

kernelcoder

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 280 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal
To add references to lib/dll in visual studio, please follow my comment in this thread -- http://forum.codecal...put-in-vc-2010/

I just tested your code on a test project: I create a project of type dynamic dll and named it ADll and put your dll code there. Then I created another project of type application and linked to the ADll following the way in that link and run it. And found that the value of that variable 'xyzzyx' is -1 in the AExe application.

Let me know if you have any confusion about it yet.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users