pass string to DLL



Hi there
I need help with c++ string
I am MQL4 programmer
I know how to pass any value to dll and return it from my language
but the string value thats I cannot pass it from my DLL

so I need C++ function that I can put it in my dll to pass string from my MQL4 like e.g (Hello) the dll will compare this string with interior string like e.g (Hi There) if match it return 1; else return 0;
in my example (Hello) Dosent match (Hi There) so my function has to return 0
can any one make a small profject for this please?
thank you
just pass it, what's the problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// mylib.hpp
#ifndef MYLIB_HPP
#define MYLIB_HPP

#include <string>

#ifdef MYLIB_EXPORTS
#define MYLIB_API __declspec(dllexport) 
#else
#define MYLIB_API __declspec(dllimport) 
#endif

bool MYLIB_API test(const std::string& str);

#endif // MYLIB_HPP 
1
2
3
4
5
6
7
// mylib.cpp
#include "mylib.hpp"

bool MYLIB_API test(const std::string& str)
{
    return (str == "Hi There");
}
1
2
3
4
5
6
7
8
9
10
11
12
// main.cpp
#include <iostream>
#include "mylib.hpp"

int main()
{
    std::cout << std::boolalpha;
    std::cout << test("Hello") << '\n'
              << test("Hi There");
              
    return 0;
}


using g++:
compile cpp file with -DMYLIB_EXPORTS (g++ -c -DMYLIB_EXPORTS mylib.cpp)
compile dll with -shared (g++ -shared -o mylib.dll mylib.o)
link dll to exectuable ( g++ main.cpp -o test.exe -L. -lmylib)
Last edited on
Mr Gamer2015
thank you very much for your help
I attach a small project for my DLL it has one function.
can you add string function to this project and re attach it for me.
int my integer function I send an number to the dll and dll compare it with 0 if the value positive will return 0 if negative will return 1;
I need to do same thing but for string values.
thank you very much
I Cannot Attach it here directory so I upload it here in 4 links please
http://www.mrkzgulf.com/do.php?id=16338
http://www.mrkzgulf.com/do.php?id=16339
http://www.mrkzgulf.com/do.php?id=16340
http://www.mrkzgulf.com/do.php?id=16341
can you add string function to this project and re attach it for me.

Nah, that's up to you, I showed you how it's done and now you should be able to do it yourself ;)
ok thank you again
I tried to do it but I got error
error C2491: 'test' : definition of dllimport function not allowed
this error in mylip.cpp
I search it for this error but I cannot solved
can you help me
1
2
3
4
5
6
7
8
9
10
// mylib.cpp
#include "StdAfx.h"
#include "mylib.hpp"

bool MYLIB_API test(const std::string& str)
{
    return (str == "Hi There");
}
// Here I Got This Error: error C2491: 'test' : 
// definition of dllimport function not allowed 
you need to compile with -DMYLIB_EXPORTS to define the MYLIB_EXPORTS flag.
When this flag is defined MYLIB_API is defined to be export, otherwise it's defined to be import. When you want to compile your dll you usually want it to be exported.
thank you
I tried to do it but still cannot do it because I am not a profissional c++. I will wait if somebody can make as full small project and upload it for me.
thank you again
you'll wait for eternity...
Do you use Visual Studio or something else?
yes sir I use Microsoft Visual Studio 2008
sir I used this code in my .h file
1
2
3
4
5
6
7
8
9
#if defined(_WIN32) || defined(_WIN64)
#   ifdef MYLIB_EXPORTS
#       define MYLIB_API __declspec(dllexport)
#   else
#       define MYLIB_API __declspec(dllimport)
#   endif
#else
#   define MYLIB_API __attribute__((visibility("default")))
#endif 


this solved that error
but the problem now when I send the string from MQL4 its always return false in your test function

when I used this
1
2
3
4
5
6
7
8
MT4_EXPFUNC int __stdcall zr(const std::string& str)
{
     const std::string& str1="Hi There";
	 const std::string& str2="Hi There";
	 //std::cout << std::boolalpha;
	if(test(str2)==test(str1))return 1;
	 else return 0;        
}


return 1
but when I use this
1
2
3
4
5
6
MT4_EXPFUNC int __stdcall zr(const std::string& str)
{
     const std::string& str1="Hi There";
	 //std::cout << std::boolalpha;
	if(test(str)==test(str1))return 1;
	 else return 0;        

always return 0 whatever the string text I sent to the DLL
int my language I say zr("Hi There");
Last edited on
please don't do this:
1
2
if(test(str)==test(str1))return 1;
	 else return 0;       


ust return the result of the comparison
return test(str)==test(str1);

this solved that error

you are on windows, right?
I don't know why that should make a difference, you still compile the same chunk of code, you don't get to the else branch on windows anyway
Thank you mr Gamer2015
I use windows 7

I tried that but that text I send from MQL4

zr("Hi There")
always return false

this is the func
1
2
3
4
MT4_EXPFUNC bool __stdcall zr(const std::string& str1) 
{
return (test(str1));
}

I think the problem with this
const std::string& str1
shold be like string str1 but this dosent work
any idea
thank you again
Last edited on
Topic archived. No new replies allowed.