简体   繁体   English

如何在Win32 C ++ Dll中连接int和char *并返回char *?

[英]How to concat int and char* and return char* in Win32 c++ Dll?

I have written a win32 DLL by using visual studio 2012. 我已经使用Visual Studio 2012编写了Win32 DLL。

My function return type is char*: 我的函数返回类型是char *:

extern "C" TESTDLL_API char* SelectColor()
{

}

I want to return string in my main application (C#): 我想在主应用程序(C#)中返回字符串:

[return: MarshalAs(UnmanagedType.LPStr)]
[DllImport("TESTDll.dll", CharSet = CharSet.Auto, SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern string SelectColor();

in body of the SelectColor method I must to concat some char* and int. 在SelectColor方法的主体中,我必须合并一些char *和int。 I tested this codes until now: 到目前为止,我一直测试此代码:

char* cid0 = GetCpuID(0);
char* cid1 = GetCpuID(1);
char* result = new char[1000];
sprintf(result,"%s%s", result ,cid0);
sprintf(result,"%s%s", result ,cid1);
return result;

This returns wrong string; 这将返回错误的字符串;

char* cid0 = GetCpuID(0);
char* cid1 = GetCpuID(1);
char * result = (char*)calloc(strlen(cid0) + strlen(cid1) + 1, sizeof(char));
strcat(result, cid0);
strcat(result, cid1);
return result;

This will fail; 这将失败;

char* cid0 = GetCpuID(0);
char* cid1 = GetCpuID(1);
stringstream ss;
ss << cid0 << cid1 << number;
return (char*)ss.str().c_str();

This returns empty string; 返回空字符串;

What I must do? 我应该做什么? Sorry for bad english. 对不起,英语不好。 Thanks. 谢谢。

i guess by : 我猜是:

char* cid0 = GetCpuID(0);
char* cid1 = GetCpuID(1);
char* result = new char[1000];
sprintf(result,"%s%s", result ,cid0);
sprintf(result,"%s%s", result ,cid1);
return result;

you want to do this; 你想这样做

char* cid0 = GetCpuID(0);
char* cid1 = GetCpuID(1);
char* result = new char[1000];
sprintf(result,"%s%s", cid0,cid1);
return result;

If you absolutely cannot change the signature of the function, then you will have to return a pointer to a static memory buffer to ensure the pointer is not leaked (in the case of your new[] and calloc() examples) or freed prematurely (in the case of your stringstream example), eg: 如果您绝对不能更改函数的签名,则必须返回一个指向静态内存缓冲区的指针,以确保该指针不会泄漏(对于new[]calloc()示例而言)或过早释放(对于您的stringstream示例),例如:

char result[1000];

extern "C" TESTDLL_API char* SelectColor()
{
    char* cid0 = GetCpuID(0);
    char* cid1 = GetCpuID(1);
    StringCchPrintfA(result, 1000, "%s%s", cid0, cid1);
    return result;
}

Or: 要么:

std::string result;

extern "C" TESTDLL_API char* SelectColor()
{
    char* cid0 = GetCpuID(0);
    char* cid1 = GetCpuID(1);
    std::ostringstream oss;
    oss << cid0 << cid1;
    result = oss.str();
    return result.c_str();
}

Note that this is not thread-safe. 请注意,这不是线程安全的。 If multiple threads need to call the function at the same time, use a memory buffer in thread-local storage, eg: 如果多个线程需要同时调用该函数,请在线程本地存储中使用内存缓冲区,例如:

__declspec(thread) char result[1000];

extern "C" TESTDLL_API char* SelectColor()
{
    char* cid0 = GetCpuID(0);
    char* cid1 = GetCpuID(1);
    StringCchPrintfA(result, 1000, "%s%s", cid0, cid1);
    return result;
}

Update : If the content of the buffer never changes, you can initialize the buffer once and just keep returning it as-is: 更新 :如果缓冲区的内容从不改变,则可以初始化缓冲区一次,并保持原样返回:

struct sResult
{
    char buffer[1000];

    sResult()
    {
        char* cid0 = GetCpuID(0);
        char* cid1 = GetCpuID(1);
        StringCchPrintfA(buffer, 1000, "%s%s", cid0, cid1);
    }
};

extern "C" TESTDLL_API char* SelectColor()
{
    static sResult result;
    return result.buffer;
}

Or: 要么:

static char result[1000];

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
    if (fdwReason == DLL_PROCESS_ATTACH)
    {
        char* cid0 = GetCpuID(0);
        char* cid1 = GetCpuID(1);
        StringCchPrintfA(buffer, 1000, "%s%s", cid0, cid1);
    }
    return TRUE;
};

extern "C" TESTDLL_API char* SelectColor()
{
    return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM