Search code examples
c++visual-studiodllmdmzinc

C++ can't identify command in DLL


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?


Solution

  • 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.