[英]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. 我正在尝试使此DLL起作用,但即使从准备好的代码中也无法使它工作。
I create a simple DLL from visual studio in c++ (win32 project) and I have this 2 files that I use. 我从c ++(win32项目)的Visual Studio中创建了一个简单的DLL,并且我拥有这2个文件。
headerZincSDK.h 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 和主项目.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 问题是它没有构建解决方案,因为它说Command1和Command2未定义
I have none to nothing knowledge on c++ and these are my first steps but I can understand much and easy. 我对C ++一无所知,这是我的第一步,但我可以理解很多东西,而且很容易。
Can anyone tell me what shall I change in these to two files to make it work? 谁能告诉我将这些文件更改为两个文件以使其正常工作吗?
Functions Command1
and Command2
are not declared in header file with that typedef
statement. 使用该
typedef
语句未在头文件中声明函数Command1
和Command2
。 That statement defines a type ZINC_COMMAND_CALLBACK
, which is a pointer to a function whose signature matches that of functions Command1
and Command2
. 该语句定义了
ZINC_COMMAND_CALLBACK
类型,该类型是指向其签名与函数Command1
和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
: 将
Command1
和Command2
整个定义放在函数OnEntry
之前,或者将定义保留在原处,并在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
. 我已经测试了此代码,它修复了与
Command1
和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. 出现的新错误是由于未定义函数
RegisterModule
和RegisterCommand
而导致的两个链接器错误,但是我猜您已省略了这些定义,因为它们与问题无关。
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
但是我设法输入了这个std :: infront并且它可以工作并且是在onEntry()函数之前
Thank you all for the quick responses 谢谢大家的快速回复
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.