1

I'm trying to export a function called CreateGameClient and when I do dumpbin /exports I get this instead ?CreateGameClient@@YAXXZ and the program I'm injecting the DLL into needs CreateGameClient and not ?CreateGameClient@@YAXXZ

And I'm using Visual Studio 2012 if that helps

This is my code ExpFunc.h

#ifndef INDLL_H
#define INDLL_H

 #ifdef EXPORTING_DLL
  __declspec(dllexport) void CreateGameClient() ;
#else
  __declspec(dllimport) void CreateGameClient() ;
#endif

#endif

Main.cpp

#include "stdafx.h"
#include <Windows.h>
#include <string.h>
#include <sstream>
#include <stdio.h>
#include <fstream>
#define EXPORTING_DLL
#include "ExpFunc.h"

void WriteLogFile(const char*);
void CreateGameClient2(int);

char* logStr;
char* CGCNStr;
char buf[250];
DWORD pid = GetCurrentProcessId();

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{

    switch (ul_reason_for_call)
    {
        case DLL_PROCESS_ATTACH:
        case DLL_THREAD_ATTACH:
            logStr = "Attached To: %d";
            sprintf(buf, logStr, pid);
            WriteLogFile(buf);
            break;
        case DLL_THREAD_DETACH:
        case DLL_PROCESS_DETACH:
            logStr = "Detached To: %d";
            sprintf(buf, logStr, pid);
            WriteLogFile(buf);
            break;
    }
    return TRUE;
}

void CreateGameClient()
{
    CreateGameClient2(2);
}

void CreateGameClient2(int num)
{
    std::stringstream temp_str;
    temp_str << (num);
    std::string str = temp_str.str();
    const char* cstr2 = str.c_str();

    sprintf(buf, cstr2, pid);
    WriteLogFile(buf);
}

void WriteLogFile(const char* szString)
{
    FILE* pFile = fopen("logFile.txt", "a");
    fprintf(pFile, "%s\n",szString);
    fclose(pFile);

}

I have tried C++ DLL Export: Decorated/Mangled names

but still didn't work

Community
  • 1
  • 1

1 Answers1

3

You must decorate both declaration and definition of a function with extern "C" __declspec(...) otherwise you should be getting a warning about non consistent linking definition and erratic behaviour.

jszpilewski
  • 1,584
  • 1
  • 20
  • 17
  • You have CreateGameClient() in the .cpp file too. Add extern "C" __declspec() to its signature to make it look exactly like in the header file. – jszpilewski Mar 29 '16 at 15:28