1

I am trying to make this DLL functionable but can't get it to work even from a ready code.

I create a simple DLL from visual studio in c++ (win32 project) and I have this 2 files that I use.

headerZincSDK.h

// headerZincSDK.h
#pragma once

#include <string>
#include <vector>

#if defined( WIN32 )
#include <tchar.h>
#define mdmT( x )   _T( x )
#else
#define mdmT( x )   L ## x
#endif

extern void OnEntry();

extern bool RegisterModule( const std::wstring& strName );

typedef struct
{
    int formId;
} 
ZincCallInfo_t;

typedef std::wstring ( *ZINC_COMMAND_CALLBACK )( const ZincCallInfo_t& info, const std::vector< std::wstring >& );

extern bool RegisterCommand( const std::wstring& strModuleName,
                     const std::wstring& strCommandName,
                     ZINC_COMMAND_CALLBACK callback );

// Helper commands for returning values
std::wstring AsString( const std::wstring& str );
std::wstring AsInteger( int value );
std::wstring AsBoolean( bool value );

And the Main Project.cpp

// Project1.cpp
#include "stdafx.h"

#include "headerZincSDK.h"

using namespace std;

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    return TRUE;
}

void OnEntry()
 {
     wstring moduleName = mdmT( "TestExt" );

     RegisterModule( moduleName );

     RegisterCommand( moduleName, mdmT( "random" ), Command1 );

     RegisterCommand( moduleName, mdmT( "reverse" ), Command2 );
 } 

wstring Command1 (const ZincCallInfo_t& info, const vector< wstring >& vParams )
 {
     //My Code
 }

wstring Command2 (const ZincCallInfo_t& info, const vector< wstring >& vParams )
 {
     //My Code
 }

The problem is that it doesn't build the solution cause it says the Command1 and Command2 are undefined

I have none to nothing knowledge on c++ and these are my first steps but I can understand much and easy.

Can anyone tell me what shall I change in these to two files to make it work?

Marko Popovic
  • 3,999
  • 3
  • 22
  • 37
Nocs
  • 71
  • 10
  • 1
    Read up on `__declspec(dllimport)` and `__declspec(dllexport)` – Erik Jan 18 '16 at 04:00
  • I don`t think it has something to do with import or export it is a step by step configuration i read and made but it doesnt work. It has to do maby with std the declaration of commands that doesnt recognize i will make the code in few lines just in case – Nocs Jan 18 '16 at 04:56
  • I'm not an epxert on C++ dlls, but shouldn't your command1 and command2 functions be declared before being used? so move those 2 functions above OnEntry() and try again. – Sujay Phadke Jan 18 '16 at 05:06
  • i have made that change too but gets me some weird errors...The 2 Commands wstring are declared if i am not wrong in the .h file with the typedef std::wstring – Nocs Jan 18 '16 at 05:07

2 Answers2

0

Functions Command1 and Command2 are not declared in header file with that typedef statement. That statement defines a type ZINC_COMMAND_CALLBACK, which is a pointer to a function whose signature matches that of functions Command1 and Command2. This means that you can take the address of one of those functions (or any others function with the same signature) and assign it to this pointer:

ZINC_COMMAND_CALLBACK comm1 = &Command1;

The problem with your code is that you are using these functions before you have declared them. Either put entire definitions for Command1 and Command2 before function OnEntry, or leave definitions where they are and add the following declarations before OnEntry:

wstring Command1(const ZincCallInfo_t& info, const vector< wstring >& vParams);
wstring Command2(const ZincCallInfo_t& info, const vector< wstring >& vParams);

I have tested this code and it fixes errors related to Command1 and Command2. New errors that appear are two linker errors caused by the fact that functions RegisterModule and RegisterCommand are not defined, but I am guessing that you omitted these definitions because they were not relevant to the question.

Marko Popovic
  • 3,999
  • 3
  • 22
  • 37
  • i will give it a try and let you know, as i said i followed a guide to create this code, i dont know how the hole thing works in c++ either total newbie on this language but i understand cause of use of other languages.Thank you in advance – Nocs Jan 18 '16 at 10:00
0

It worked but only with std infront of them

std::wstring Command1 (const ZincCallInfo_t& info, const vector< std::wstring >& vParams )
 {
     //My Code
 }

std::wstring Command2 (const ZincCallInfo_t& info, const vector< std::wstring >& vParams )
 {
     //My Code
 }

Otherwise it was full of errors dont know why. But i manage to enter this std:: infront and it works and yes before the onEntry() function

Thank you all for the quick responses

Nocs
  • 71
  • 10
  • Namespace qualification (adding `std::` before type) is only necessary if you have removed the statement `using namespace std;` from "Main Project.cpp". – Marko Popovic Jan 18 '16 at 12:28
  • Yes indeed, i have removed it that, didnt mention it cause i really dont know all of them what they do and i manage after many tries to achieve it. – Nocs Jan 18 '16 at 17:34