a simple but crashed code

A

acoder1983

why the code fails
composed of two projects(compiled by VC6)
Test.dll (Test.h, Test.cpp)
Main.exe (Main.cpp)dependent by Test.dll

Test.dll
//----------------------------------------------------
// Test.h

#pragma once

#ifdef TEST_EXPORTS
#define TEST_API __declspec(dllexport)
#else
#define TEST_API __declspec(dllimport)
#endif

#include "Test.h"

#include <vector>
using namespace std;

class TEST_API Test
{
public:
void PushInts(vector<int>& vec, int num);
};

TEST_API void PushInts2(vector<int>& vec, int num);

//-----------------------------------------------------



//-----------------------------------------------------
// Test.cpp

#define TEST_EXPORTS
#include <windows.h>
#include "Test.h"

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

void Test::pushInts(vector<int>& vec, int num)
{
vec.push_back(num);
}

void PushInts2(vector<int>& vec, int num)
{
vec.push_back(num);
}
//-----------------------------------------------------

Main.exe
//-----------------------------------------------------
#include "Test.h"

int main(int argc, char* argv[])
{
vector<int> vec;
Test t;
t.PushInts(vec, 1);
return 0;
}
//-----------------------------------------------------
 
B

Bob Hairgrove

comments inline further down...

why the code fails
composed of two projects(compiled by VC6)
Test.dll (Test.h, Test.cpp)
Main.exe (Main.cpp)dependent by Test.dll

Test.dll
//----------------------------------------------------
// Test.h

#pragma once

#ifdef TEST_EXPORTS
#define TEST_API __declspec(dllexport)
#else
#define TEST_API __declspec(dllimport)
#endif

#include "Test.h"

This IS "Test.h", isn't it?
#include <vector>
using namespace std;

You are polluting the global namespace by putting this in a header file.
class TEST_API Test
{
public:
void PushInts(vector<int>& vec, int num);
};

TEST_API void PushInts2(vector<int>& vec, int num);

//-----------------------------------------------------



//-----------------------------------------------------
// Test.cpp

#define TEST_EXPORTS
#include <windows.h>
#include "Test.h"

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

void Test::pushInts(vector<int>& vec, int num)
{
vec.push_back(num);
}

void PushInts2(vector<int>& vec, int num)

This should be:
void TEST_API PushInts2(vector said:
{
vec.push_back(num);
}
//-----------------------------------------------------

Main.exe
//-----------------------------------------------------
#include "Test.h"

int main(int argc, char* argv[])
{
vector<int> vec;
Test t;
t.PushInts(vec, 1);
return 0;
}
//-----------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top