简体   繁体   中英

Win32 DLL Not Returning correct data

I'm very new to C++ programming. I have a 3rd party app that can utilize Win32 DLLs. The test project I'm working on compiles without error, but in use doesn't seem to actually return the expected data.

With the below function Foo, the expectation is that the char array that is passed in will return the same values.

Instead, regardless of the data passed in my 3rd party app only sees the return value: 0

My suspicion is that I am not using the pointer 'char *data_out' correctly.


DemoLib.h:

#pragma once

#define DLL_EXP extern "C" __declspec(dllexport)
DLL_EXP void Foo(char* data_in, char *data_out);

DemoLib.cpp:

#include "stdafx.h"
#include "DemoLib.h"
#include <iostream>

DLL_EXP void Foo(char* data_in, char *data_out)
{
    int a_size = sizeof(data_in) / sizeof(char);
    std::string s_a = convertToString(data_in, a_size);

    char strArray[100];
    strcpy_s(strArray, s_a.c_str());

    data_out = strArray;
}

std::string convertToString(char* a, int size)
{
    int i;
    std::string s = "";
    for (i = 0; i < size; i++) {
        s = s + a[i];
    }
    return s;
}

Exports.def:

LIBRARY DemoLib
EXPORTS
   Foo   @1

There are a couple of problems in your code. First, the line:

int a_size = sizeof(data_in) / sizeof(char);

will not give you the length of the data_in string! Rather, it will give you a (fixed) value that is the size of a pointer divided by the size of a char. So, assuming you have a C-style, null-terminated string, use this:

int a_size = int(strlen(data_in));

Second, your line:

data_out = strArray;

does not copy the string data from strArray to data_out ! Rather, it simply replaces the pointer value (address) that data_out holds with the address of the (local) strArray array. (However, this will not change the value of any pointer in the calling module.)

What you need to do, here, is actually copy the data directly from the std::string into the string pointed to by the data_out argument (assuming it is a big enough buffer).

With these changes in mind, your Foo function could look like this:

DLL_EXP void Foo(char* data_in, char *data_out)
{
    int a_size = int(strlen(data_in)); // Note: "strlen" returns a "size_t" (unsigned) type
    std::string s_a = convertToString(data_in, a_size);
    strcpy(data_out, s_a.c_str());
}


Alternatively, if you actually want the function to return a pointer to some local data, then you will: (a) need to declare that local data static ; and (b) make the corresponding argument a pointer-to-pointer :

 DLL_EXP void Foo(char* data_in, char** data_out) { int a_size = int(strlen(data_in)); // Note: "strlen" returns a "size_t" (unsigned) type std::string s_a = convertToString(data_in, a_size); static char strArray[100]; strcpy(strArray, s_a.c_str()); *data_out = strArray; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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