Sign in to follow this  
Bombshell93

[C++] Creating and using a DLL

Recommended Posts

Bombshell93    245
I'm in the process of writing a Game Engine. I plan on making it easily distributed without giving out the raw source code.
Obviously putting the code in a DLL came to mind.
Problem is I'm still fairly new to C++ (I've been working with XNA and C# for a while) So I've been running into a number of problems.

at the moment all I really want to do is make a DLL using Indigo.h and Indigo.cpp
I've tried but constantly running into problems, googles served me well for most but I've come to a rather unnerving halt.

I'm getting the following error (and other variations of it, same error, different functions)
[code]1>MProg.obj : error LNK2019: unresolved external symbol "public: void __thiscall Indigo::DXManager::CleanD3D(void)" (?CleanD3D@DXManager@Indigo@@QAEXXZ) referenced in function _WinMain@16[/code]

To my understanding I need to make a .lib file, but I can't find anywhere clearly explaining the whole process.

Any help is appreciated,
Thanks in advanced,
Bombshell

Share this post


Link to post
Share on other sites
icuit    104
[quote name='bombshell93' timestamp='1310953356' post='4836588']
I'm in the process of writing a Game Engine. I plan on making it easily distributed without giving out the raw source code.
Obviously putting the code in a DLL came to mind.
Problem is I'm still fairly new to C++ (I've been working with XNA and C# for a while) So I've been running into a number of problems.

at the moment all I really want to do is make a DLL using Indigo.h and Indigo.cpp
I've tried but constantly running into problems, googles served me well for most but I've come to a rather unnerving halt.

I'm getting the following error (and other variations of it, same error, different functions)
[code]1>MProg.obj : error LNK2019: unresolved external symbol "public: void __thiscall Indigo::DXManager::CleanD3D(void)" (?CleanD3D@DXManager@Indigo@@QAEXXZ) referenced in function _WinMain@16[/code]

To my understanding I need to make a .lib file, but I can't find anywhere clearly explaining the whole process.

Any help is appreciated,
Thanks in advanced,
Bombshell
[/quote]

your declare and define are mismatching.
you mentioned the lib file.it maybe [b]Dynamic or Static lib.[/b]
it depends on what type you compiled.
if [b]Static,[/b]you simply need a .h file and the lib file.
[code][color=#333333][font=Arial][size=4]#include "..\lib.h"[/size][/font][/color]
[color=#333333][font=Arial][size=4]#pragma comment(lib,"..\\debug\\libTest.lib") [/size][/font][/color][/code]

if [b]Dynamic,[/b] you need the dll & lib or only dll.
you have to location the function address by [font=Arial][size=4]GetMuduleHandle and [/size][/font][font=Arial][size=4]GetProcAddress.[/size][/font]
[font=Arial][size=4]
[/size][/font]
[font=Arial][size=4]
[/size][/font]

Share this post


Link to post
Share on other sites
Bombshell93    245
I'm running into masses of errors, so I'm sure I'm not getting a proper grasp on this.
Here are the errors I'm getting

[code]1>------ Build started: Project: IndigoFramework, Configuration: Debug Win32 ------
1> Indigo.cpp
1>c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\Indigo.h(24): error C2059: syntax error : 'string'
1>c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\Indigo.h(24): error C2238: unexpected token(s) preceding ';'
1>c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\Indigo.h(26): error C2059: syntax error : 'string'
1>c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\Indigo.h(26): error C2238: unexpected token(s) preceding ';'
1>c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\Indigo.h(28): error C2059: syntax error : 'string'
1>c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\Indigo.h(28): error C2238: unexpected token(s) preceding ';'
1>..\IndigoEngine\Indigo.cpp(3): error C2039: 'InitD3D' : is not a member of 'Indigo::DXManager'
1> c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\Indigo.h(18) : see declaration of 'Indigo::DXManager'
1>..\IndigoEngine\Indigo.cpp(10): warning C4244: '=' : conversion from 'float' to 'UINT', possible loss of data
1>..\IndigoEngine\Indigo.cpp(11): warning C4244: '=' : conversion from 'float' to 'UINT', possible loss of data
1>..\IndigoEngine\Indigo.cpp(26): error C2065: 'swapchain' : undeclared identifier
1>..\IndigoEngine\Indigo.cpp(27): error C2065: 'dev' : undeclared identifier
1>..\IndigoEngine\Indigo.cpp(29): error C2065: 'devcon' : undeclared identifier
1>..\IndigoEngine\Indigo.cpp(32): error C2065: 'swapchain' : undeclared identifier
1>..\IndigoEngine\Indigo.cpp(32): error C2227: left of '->GetBuffer' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>..\IndigoEngine\Indigo.cpp(34): error C2065: 'dev' : undeclared identifier
1>..\IndigoEngine\Indigo.cpp(34): error C2227: left of '->CreateRenderTargetView' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>..\IndigoEngine\Indigo.cpp(34): error C2065: 'backbuffer' : undeclared identifier
1>..\IndigoEngine\Indigo.cpp(37): error C2065: 'devcon' : undeclared identifier
1>..\IndigoEngine\Indigo.cpp(37): error C2227: left of '->OMSetRenderTargets' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>..\IndigoEngine\Indigo.cpp(37): error C2065: 'backbuffer' : undeclared identifier
1>..\IndigoEngine\Indigo.cpp(47): error C2065: 'devcon' : undeclared identifier
1>..\IndigoEngine\Indigo.cpp(47): error C2227: left of '->RSSetViewports' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>..\IndigoEngine\Indigo.cpp(50): error C2039: 'RenderFrame' : is not a member of 'Indigo::DXManager'
1> c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\Indigo.h(18) : see declaration of 'Indigo::DXManager'
1>..\IndigoEngine\Indigo.cpp(52): error C2065: 'devcon' : undeclared identifier
1>..\IndigoEngine\Indigo.cpp(52): error C2227: left of '->ClearRenderTargetView' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>..\IndigoEngine\Indigo.cpp(52): error C2065: 'backbuffer' : undeclared identifier
1>..\IndigoEngine\Indigo.cpp(54): error C2065: 'swapchain' : undeclared identifier
1>..\IndigoEngine\Indigo.cpp(54): error C2227: left of '->Present' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>..\IndigoEngine\Indigo.cpp(57): error C2039: 'CleanD3D' : is not a member of 'Indigo::DXManager'
1> c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\Indigo.h(18) : see declaration of 'Indigo::DXManager'
1>..\IndigoEngine\Indigo.cpp(59): error C2065: 'swapchain' : undeclared identifier
1>..\IndigoEngine\Indigo.cpp(59): error C2227: left of '->SetFullscreenState' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>..\IndigoEngine\Indigo.cpp(61): error C2065: 'swapchain' : undeclared identifier
1>..\IndigoEngine\Indigo.cpp(61): error C2227: left of '->Release' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>..\IndigoEngine\Indigo.cpp(62): error C2065: 'backbuffer' : undeclared identifier
1>..\IndigoEngine\Indigo.cpp(62): error C2227: left of '->Release' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>..\IndigoEngine\Indigo.cpp(63): error C2065: 'dev' : undeclared identifier
1>..\IndigoEngine\Indigo.cpp(63): error C2227: left of '->Release' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>..\IndigoEngine\Indigo.cpp(64): error C2065: 'devcon' : undeclared identifier
1>..\IndigoEngine\Indigo.cpp(64): error C2227: left of '->Release' must point to class/struct/union/generic type
1> type is ''unknown-type''
2>------ Build started: Project: IndigoEngine, Configuration: Debug Win32 ------
2> MProg.cpp
2>c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\indigo.h(24): error C2059: syntax error : 'string'
2>c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\indigo.h(24): error C2238: unexpected token(s) preceding ';'
2>c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\indigo.h(26): error C2059: syntax error : 'string'
2>c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\indigo.h(26): error C2238: unexpected token(s) preceding ';'
2>c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\indigo.h(28): error C2059: syntax error : 'string'
2>c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\indigo.h(28): error C2238: unexpected token(s) preceding ';'
2>c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\mprog.cpp(61): error C2039: 'InitD3D' : is not a member of 'Indigo::DXManager'
2> c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\indigo.h(18) : see declaration of 'Indigo::DXManager'
2>c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\mprog.cpp(77): error C2039: 'RenderFrame' : is not a member of 'Indigo::DXManager'
2> c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\indigo.h(18) : see declaration of 'Indigo::DXManager'
2>c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\mprog.cpp(79): error C2039: 'CleanD3D' : is not a member of 'Indigo::DXManager'
2> c:\users\bombshell\documents\visual studio 2010\projects\indigoengine\indigoengine\indigo.h(18) : see declaration of 'Indigo::DXManager'========== Build: 0 succeeded, 2 failed, 0 up-to-date, 0 skipped ==========[/code]

I'm sure you don't need the full code, but just for debugs sake.


MProg.cpp (program)
[code]#include <Windows.h>
#include <WindowsX.h>
#include "Indigo.h"
#pragma comment (lib, "Indigo.lib")
using namespace Indigo;

#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600

DXManager dxManager;

LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}

return DefWindowProc(hWnd, message, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hInstancePrev, LPSTR lPCmdLn, int nCmdShow)
{

HWND hWnd;
WNDCLASSEX wc;

ZeroMemory(&wc,sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WinProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = "WindowClass1";

RegisterClassEx(&wc);

RECT wr = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);

hWnd = CreateWindowEx(
NULL,
"WindowClass1",
"IndigoEngine",
WS_OVERLAPPEDWINDOW,
300, 300,
SCREEN_WIDTH, SCREEN_HEIGHT,
NULL,
NULL,
hInstance,
NULL
);

ShowWindow(hWnd, nCmdShow);

dxManager.InitD3D(hWnd, 1, DXGI_FORMAT_B8G8R8A8_UNORM, SCREEN_WIDTH, SCREEN_HEIGHT);

MSG msg = {0};

while (TRUE)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);

if (msg.message == WM_QUIT)
{
break;
}
}
dxManager.RenderFrame(D3DXCOLOR(1.0f,0.0f,1.0f,1.0f));
}
dxManager.CleanD3D();

return msg.wParam;
}[/code]


Indigo.h (Library)
[code]#ifndef INDIGO_H
#define INDIGO_H

#include <Windows.h>
#include <WindowsX.h>

#include <d3d11.h>
#include <d3dx11.h>
#include <d3dx10.h>

#pragma comment (lib, "d3d11.lib")
#pragma comment (lib, "d3dx11.lib")
#pragma comment (lib, "d3dx10.lib")

namespace Indigo
{
class DXManager
{
IDXGISwapChain *swapchain;
ID3D11RenderTargetView *backbuffer;
ID3D11Device *dev;
ID3D11DeviceContext *devcon;
public:
extern "C" __declspec(dllexport) void InitD3D(HWND hWnd, UINT BufferCount, DXGI_FORMAT BufferFormat, float SCREEN_WIDTH, float SCREEN_HEIGHT);

extern "C" __declspec(dllexport) void RenderFrame(D3DXCOLOR Colour);

extern "C" __declspec(dllexport) void CleanD3D(void);
};
};#endif[/code]

Indigo.cpp (Library)
[code]#include "Indigo.h"

extern "C" __declspec(dllexport) void Indigo::DXManager::InitD3D(HWND hWnd, UINT BufferCount, DXGI_FORMAT BufferFormat, float SCREEN_WIDTH, float SCREEN_HEIGHT)
{
DXGI_SWAP_CHAIN_DESC scd;
ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));

scd.BufferCount = BufferCount;
scd.BufferDesc.Format = BufferFormat;
scd.BufferDesc.Width = SCREEN_WIDTH;
scd.BufferDesc.Height = SCREEN_HEIGHT;
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
scd.OutputWindow = hWnd;
scd.SampleDesc.Count = 4;
scd.Windowed = TRUE;
scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;

D3D11CreateDeviceAndSwapChain(NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
&scd,
&swapchain,
&dev,
NULL,
&devcon);

ID3D11Texture2D *pBackBuffer;
swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);

dev->CreateRenderTargetView(pBackBuffer, NULL, &backbuffer);
pBackBuffer->Release();

devcon->OMSetRenderTargets(1, &backbuffer, NULL);

D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));

viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = SCREEN_WIDTH;
viewport.Height = SCREEN_HEIGHT;

devcon->RSSetViewports(1, &viewport);
}

extern "C" __declspec(dllexport) void Indigo::DXManager::RenderFrame(D3DXCOLOR Colour)
{
devcon->ClearRenderTargetView(backbuffer, Colour);

swapchain->Present(0, 0);
}

extern "C" __declspec(dllexport) void Indigo::DXManager::CleanD3D(void)
{
swapchain->SetFullscreenState(FALSE, NULL);

swapchain->Release();
backbuffer->Release();
dev->Release();
devcon->Release(); }[/code]

Please let me know where I'm going wrong in as much detail as possible, anything that can help stop me from making these mistakes in future would be of great use.
Also on a similar subject, I came across (when googling some errors) a code snippet of a context that seems quite helpful.
[code]#ifndef __cplusplus
extern "c++" {
#endif

#include "test.h"

#ifndef __cplusplus
}
#endif[/code]
The code was intended for importing C++ code into C,
but would this work in my case?

Thanks again for the help,
Bombshell

Share this post


Link to post
Share on other sites
Bombshell93    245
thanks, that's got rid of most of the errors (I hope)
But I'm still getting this
[code]1>------ Build started: Project: IndigoFramework, Configuration: Debug Win32 ------
1> Indigo.cpp
1>..\IndigoEngine\Indigo.cpp(10): warning C4244: '=' : conversion from 'float' to 'UINT', possible loss of data
1>..\IndigoEngine\Indigo.cpp(11): warning C4244: '=' : conversion from 'float' to 'UINT', possible loss of data
1> Creating library C:\Users\Bombshell\documents\visual studio 2010\Projects\IndigoEngine\Debug\IndigoFramework.lib and object C:\Users\Bombshell\documents\visual studio 2010\Projects\IndigoEngine\Debug\IndigoFramework.exp
1> IndigoFramework.vcxproj -> C:\Users\Bombshell\documents\visual studio 2010\Projects\IndigoEngine\Debug\IndigoFramework.dll
2>------ Build started: Project: IndigoEngine, Configuration: Debug Win32 ------
2> MProg.cpp
2>LINK : fatal error LNK1104: cannot open file 'Indigo.lib'========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========[/code]

I've checked the build folder and the .lib and .exp is being made.
So why would it not open?
Do I have anything to set in the project settings?

Share this post


Link to post
Share on other sites
ApochPiQ    23005
Yes, you have to make sure that your linker is set to look for .lib files in whatever path the librarian is generating the .libs to. Unfortunately, there's no special smarts in the tools that can detect that for you, which means you have to resort to a little bit of manual configuration. Thankfully, though, it's pretty easy :-)

Share this post


Link to post
Share on other sites
Bombshell93    245
Its working perfectly!!
I made the assumption that the target path would be the default.
I did a google for some solutions and I noticed 1 or 2 where they forgot about adding in path macros,
So I gave it a try and
$(TargetDir)IndigoFramework.lib
did the trick!

Thanks for the help :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this