char*/string function type

Hi folks!

I'm trying to write DLL with a function that accepts a string, manipulates it and returns shorter version of it. Previously I was able to create similar DLL that accepts and returns "int" type, but for "string/char" type I can't figure out how to do it.

My code in my_dll.cpp
1
2
3
4
5
 char* my_func(char* txt_in) {
    // ... string manipulation ...
    // char* txt_out = ...
    return txt_out;
    }

in my_dll.h
 
extern "C" my_dll_API char* my_func(char*);


I tried using 'std:string'/'char[]'/'char*'/'const char*' as a type of function and argument, but compiler just wouldn't accept it.
I.e. when I try using type 'char*' I get error
 
Warning	C4273	'my_func': inconsistent dll linkage


If somebody knowledgeable could point out what I'm doing wrong here, I'll be very grateful.
Also, generally, what would be the most suitable data type here? (I intend to use this DLL function for long strings ~200MB)

Thanks!
The definition and declaration must match. I am not sure what my_dll_API is,
normally you would use __declspec(dllexport)

This works for VS2019:
my_dll.h
1
2
3
#pragma once

extern "C" __declspec(dllexport) char* my_func(char *input);



my_dll.cpp
1
2
3
4
5
6
7
#include "my_dll.h"

extern "C" __declspec(dllexport) char* my_func(char *input)
{
  // modify input
  return input;
}


A char* seems apprpiate since you pass only a pointer to it.
I am not sure what my_dll_API is,

Most likely it's a macro to switch between dllimport and dllexport

1
2
3
4
5
6
7
8
9
#ifdef MY_DLL
#ifdef MY_DLL_EXPORTS
#define MY_DLL_API __declspec(dllexport)
#else
#define MY_DLL_API __declspec(dllimport)
#endif
#else
#define MY_DLL_API
#endif 

(It's been a while since I've done this so I forget why that's necessary)
Last edited on
what would be the most suitable data type here


See https://stackoverflow.com/questions/5347355/can-i-pass-stdstring-to-a-dll
@thmm
Thank you, I used your code and it works now! You are awesome!

@seeplus
Thanks for the link, it is relevant to my project.

@Ganado
Being a noob, I didn't even realize what it is, apparently it's just what you said, a macro to switch between dllimport and dllexport

Thanks!
Topic archived. No new replies allowed.