Register
Handmade Hero»Forums»Code»dynamic code loading can't open .dll
63 posts
dynamic code loading can't open .dll
1 month, 1 week ago Edited by C_Worm on April 13, 2020, 3:21 p.m. Reason: Initial post
hey, Im trying to dynamically load code during runtime like casey, but i keep getting this linker error LNK1104: cannot open 'dll.dll'

I think im doing it like casey but can't find out why it won't let me open the .dll file.


I am running the exe from its directory, and i see that the 'newDLL.dll' IS created. Problem is that i just can't compile it during runtime.

None of the 'printf("fail");' is generated aswell which should suggest that everythings is working fine right?


main:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
	HMODULE gameObjectDLL = LoadLibrary("dll.dll");
	PFNCHANGEX *changeX;
	int loadTime = 120;
	int currLoadTime = loadTime;

	while(!glfwWindowShouldClose(window) && running) 
	{

		if(currLoadTime++ == loadTime)
		{
			if(gameObjectDLL)
			{
				if(!FreeLibrary(gameObjectDLL))
				{
					printf("Freeding library Failed\n");
				}
			}

			if(!CopyFile("dll.dll", "newDLL.dll", FALSE))
			{
				printf("Copy DLL Failed\n");
			}

			gameObjectDLL = LoadLibrary("newDLL.dll");

			if(!gameObjectDLL)
			{
				printf("DLL LOAD FAIL\n");
			}

			changeX = (PFNCHANGEX*)GetProcAddress(gameObjectDLL, "ChangeX");
			if(!changeX)
			{
				printf("GetProc FAIL\n");
			}
			currLoadTime = 0;
		}
}


build.bat:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
@echo off

pushd ..\build

SET includePath=C:\Code\OpenGL\BoardGame\include
SET CJGLPath=C:\Code\OpenGL\CJGL

cl -nologo ..\src\include\dll.cpp -LD
cl -nologo /I %includePath% /I %CJGLPath% /F 500000000 /EHsc /MD -nologo -Zi -wd4477 -wd4005 ..\src\BoardGame.cpp user32.lib gdi32.lib opengl32.lib glfw3.lib shell32.lib

popd

copy /Y shader\vertexShader.glsl 	..\build\shader
copy /Y shader\fragmentShader.glsl 	..\build\shader
copy /Y shader\textFS.glsl 		..\build\shader
copy /Y shader\solidColorFS.glsl 	..\build\shader


del *.un~ *.cpp~ *.h~ *.bat~ *.glsl~ *.txt *.txt~ include\*.h~ include\*.un~ include\*.h~~ ..\assets\*.un~ ..\assets\*.c~
Mārtiņš Možeiko
2072 posts / 1 project
dynamic code loading can't open .dll
1 month, 1 week ago
I'm guessing linker is called while CopyFile("dll.dll", "newDLL.dll", FALSE) operation is happening. When file is being copied, it cannot be overwritten. Windows kind of "locks" it.
To workaround this, you'll need "lock" file management yourself. Check later episodes where Casey implement it.