issue to pass string to dll C - page 2

 
Your statement is simply wrong.

Posting on an old thread and providing flawed information is not helpful to anyone.

Take a look for windows environment:


Btw, there is no such thing as a cli c++ lib.

Where have you got your Infos from?
 
JC: if a string is passed by reference, the address of the buffer of this string without copying it is passed to the function imported from DLL. 

really helps! (compiled with MinGW release_link_options: -Wl,--kill-at)

dll.h

#ifndef _myDllMQL_myDllMQL_h
#define _myDllMQL_myDllMQL_h

#define DllImport extern "C" __declspec(dllimport)
#define DllExport extern "C" __declspec(dllexport)

#ifdef flagDLL
        #define DLLIMPORT __declspec(dllexport)
#else
        #define DLLIMPORT __declspec(dllimport)
#endif


#ifdef __cplusplus
extern "C" {
#endif
 
DLLIMPORT bool __stdcall fnReplaceString(wchar_t *text,  wchar_t *from,wchar_t *to);
 
#ifdef __cplusplus
}
#endif

#endif

dll.cpp

#include <windows.h>

#include <wchar.h>

#include "myDllMQL.h"

BOOL APIENTRY DllMain(HANDLE hModule, DWORD reason,  LPVOID lpReserved)
{
	switch (reason) {
	case DLL_PROCESS_ATTACH:
		break;
	case DLL_PROCESS_DETACH:
		break;
	case DLL_THREAD_ATTACH:
		break;
	case DLL_THREAD_DETACH:
		break;
	}
	return TRUE;
}

DLLIMPORT bool __stdcall fnReplaceString(wchar_t *text,  wchar_t *from, wchar_t *to)
  {
   wchar_t *cp;
//--- check params
   if(text==NULL || from==NULL || to==NULL) return false;
   if(wcslen(from)!=wcslen(to))             return false;
//--- find substr
   if((cp=wcsstr(text,from))==NULL)         return false;
//--- change
   memcpy(cp,to,wcslen(to)*sizeof(wchar_t));
   
   return true;
  }

mql4 - import from Library folder

#import "myDllMQL.dll"
bool fnReplaceString(string& text,string from,string to);
#import

use

//--- change string
// NOT using MqlStruct... ??
   string text="A quick brown fox jumps over the cat"; 
   
   bool b=fnReplaceString(text,"fox","DOG");
   Print(b," Replace: ",text);
but do not clearly understand how to use struct MqlStr in this code? (though it works as is)
 
Dominik Egert:

Btw, there is no such thing as a cli c++ lib.

Where have you got your Infos from?

cli c++ is used in MS Visual Studio - writing c++ libs in NET... you'd better to get acquanted with a whole .NET-libs infrastructure to deal with it... but really, I also lack experience with Visual Studio -- therefore will not debate this topic...

just have written about calling-conventions' differences in past & my suppositions over now

 
Well, a Lib has no execution context. That's why it is called a lib.

When binding it to something like a cli executable, it gets a context in which it is used, but that could also be a gui application.

That's why it is a lib, or a dll.

.net has a different prerequisite of loading such a lib, there are different contextual loading environments, which require certain functions/configurations to be present in the lib.

A lib is a lib, independent of it's context it gets loaded to.
 
Dominik Egert #: 
.net has a different prerequisite of loading such a lib, there are different contextual loading environments,

NOT exactly... you can use C#.NET libs in VB.NET-Projects & vc.vs. ... I assume & am almost sure, that such concept is accepted by Microsoft when creating all its NET-libs & even those that are not created yet (I mean any that a developer can create himself) - inter_compatibility is much more better in NET.infrastructure now, than it was earlier in before-NET-times  

Reason: