Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: dynamic code loading problem  (Read 1748 times)

0 Members and 1 Guest are viewing this topic.

C_Worm

  • Newbie
  • *
  • Posts: 18
    • View Profile
dynamic code loading problem
« on: August 13, 2019, 01:08:55 pm »
When I change som attributes in the gameCode.cpp it does seem to succesfully reload the DLL during runtime
however, i don't see the changes on screen, that was made in the code.

i want it to reload during runtime.

CJsfml_002.cpp
#define SFML_STATIC
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include "SFML\Graphics.hpp"

#include <iostream>

float vel = 7.0f;

typedef void (*rectFP)(sf::RectangleShape *);

int main()
{
        sf::RenderWindow window(sf::VideoMode(960, 540), "SFML works!");
        window.setFramerateLimit(60);  
       
        sf::RectangleShape mRect(sf::Vector2f(40.0f, 40.0f));;
        mRect.setPosition(sf::Vector2f(30.0f, 30.0f)); 
        mRect.setFillColor(sf::Color::Green);

        // Getting pointer to the rect
        sf::RectangleShape *rect = &mRect;


        while (window.isOpen())
    {
                int count = 125;
               
               // Loading DLL
                CopyFile("gameCode.dll", "temp_gameCode.dll", false);
                HMODULE hm = LoadLibrary("temp_gameCode.dll");

                if(hm)
                {
                                std::cout << "LOAD LIB SUCCESS!\n";
                }
               
               // Loading DLL function
                rectFP rfp = (rectFP)GetProcAddress(hm, "CreateRect");

                if(rfp)
                {
                                std::cout << "load func SUCCESS!\n";
                }

               // Unloads DLL
                FreeLibrary(hm);
               
                // Using the DLL function
                rfp(rect);

        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
       
        // Drawing the rect that was changed by the DLL-function
        window.draw(*rect);

        window.display();

    }

    return 0;
}

 

gameCode.h
#define SFML_STATIC
#include "SFML\Graphics.hpp"

extern "C"
{
        __declspec(dllexport) void CreateRect(sf::RectangleShape *RS);
}      

 

gameCode.cpp
#include "gameCode.h"

__declspec(dllexport) void CreateRect(sf::RectangleShape *RS)
{
        RS->setSize(sf::Vector2f(100.0f, 200.0f));     
        RS->setFillColor(sf::Color::Blue);
        RS->setPosition(sf::Vector2f(100.0f, 100.0f));
}

 

build.bat
@echo off

:: IMPORTANT!  "=" has to come IMMEDIATLEY after VARIABLE NAME

SET compilerSwitches= /MD /EHsc -Zi -nologo
SET winLibs= user32.lib gdi32.lib winmm.lib Opengl32.lib ws2_32.lib advapi32.lib

SET sfmlDependLibsX64= /LIBPATH:"C:\SFML\SFML-2.5.1_x64\lib" freetype.lib openal32.lib flac.lib vorbisenc.lib vorbisfile.lib vorbis.lib ogg.lib
SET sfmlDependLibsX86= /LIBPATH:"C:\SFML\SFML-2.5.1_x86\lib" freetype.lib openal32.lib flac.lib vorbisenc.lib vorbisfile.lib vorbis.lib ogg.lib

SET sfmlLibsX64= sfml-system-s.lib sfml-graphics-s.lib sfml-window-s.lib sfml-audio-s.lib sfml-network-s.lib
SET sfmlLibsX86= sfml-system-s.lib sfml-graphics-s.lib sfml-window-s.lib sfml-audio-s.lib sfml-network-s.lib

If NOT EXIST ..\buildx64 mkdir ..\buildx64
If NOT EXIST ..\buildx86 mkdir ..\buildx86
If NOT EXIST ..\assets mkdir ..\assets

 pushd ..\buildx64
 
 cl %compilerSwitches% /I ..\include ..\src\CJsfml_002.cpp %winLibs% /link %sfmlDependLibsX64% %sfmlLibsX64%
 cl %compilerSwitches% /I ..\include ..\src\gameCode.cpp /LD %winLibs% /link %sfmlDependLibsX64% %sfmlLibsX64%
 
 popd
 
 @echo x64 build Done!
 
 ::------------------ x86 BUILD (set vcvarsx86) -----------------

 ::pushd ..\buildx86
 
 ::cl /MD /EHsc -Zi -nologo /I ..\include ..\src\CJsfml_002.cpp %winLibs% /link %sfmlDependLibsX86% %sfmlLibsX86%
 
 ::popd
 ::
 ::@echo x86 build Done!

del *.~ *.cpp~ *.bat~ *.un~ *.h~

 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: dynamic code loading problem
« Reply #1 on: August 13, 2019, 05:52:08 pm »
Try to call the DLL function before unloading the DLL.
Laurent Gomila - SFML developer

C_Worm

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: dynamic code loading problem
« Reply #2 on: August 13, 2019, 06:17:09 pm »
Try to call the DLL function before unloading the DLL.

I’ve tried that, it doesn’t make a difference.