Hi, I have two projects (Projects A and B). Project A is a dll project, defining a function called "regex".
Project B dynamically loads this DLL, and calls Project A's "regex" function via LoadLibrary/GetProcAddress.
Regex takes a pointer to an std::vector (std::vector<std::cmatch>).
When debugging ProjectB, I can see that, within the code from ProjectA (in the "regex" call), a loop that loops through the elements of the vector outputs all the elements in the vector to console as expected. But the loop in ProjectB ( which executes after ProjectA), which also loops through the vector, and, is supposed to output the elements of the vector, outputs empty strings, not, as I would expect, the same strings (which contain results), as in the loop in Project A.
How is this happening. Does this have anything to do with it being a DLL, and, maybe, somehow values/memory addresses (or something similar) of the vector/its elements being destructed across the Projects/Dlls? Does anyone have any idea how to get rid of this?
Thanks! :)
C:)
Output and Code See Below:
Output Loop in A:
Output Loop in B: (empty) (i.e. none)
Project A DLL Header (interface.h):
1 2 3 4 5 6 7
|
#include "stdafx.h"
#include <vector>
#include <regex>
extern "C" {__declspec(dllexport) int __cdecl regex(std::string target,std::string rgx, std::vector<std::cmatch*>* matches);}
|
Project A DLL cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
// regex.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "interface.h"
#include "windows.h"
#include <iostream>
extern "C" {
__declspec(dllexport) int __cdecl regex(std::string target,std::string rgx, std::vector<std::cmatch*>* matches)
{
// this can be done using raw string literals:
// const char *reg_esp = R"([ ,.\t\n;:])";
std::regex rgxa = std::regex(rgx);
const char *targetA = target.c_str();
for(auto it = std::cregex_iterator(targetA, targetA + std::strlen(targetA), rgxa);
it != std::cregex_iterator();
++it)
{
std::cmatch match = *it;
matches->push_back(&match); .
std::cout << match.str() << '\n';
}
//Output of loop:
// Un
// Un
//output End
return EXIT_SUCCESS;
}
}
|
Project B calling code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
HINSTANCE hGetProcIDDLL = LoadLibrary(L"C:\\Users\\a\\Documents\\Visual Studio 2012\\Projects\\regex\\Debug\\regex.dll"); //<--- load DLL (works)
FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"regex");//<--- Get proc id, works
typedef int (__cdecl * pICFUNCRegex)(std::string, std::string, std::vector<std::cmatch*>*);
pICFUNCRegex regexP;
regexP = pICFUNCRegex(lpfnGetProcessID);
std::vector<std::cmatch*>* matches = new std::vector<std::cmatch*>;
intMyReturnVal = regexP("Unseen University", "Un", matches);
for(int i = 0; i < matches->size(); i++)
{
std::cout << matches->at(i)->str() << std::endl;
}
//output of loop: empty
|