Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3042

DLL C++ and C#

$
0
0
Hi,

I'm writing a dll in C++ to get the serial hd, this is the code of the function:
Code:

__declspec(dllexport) char* __stdcall char* GetSerialHD()
{
    WmiQueryResult res = getWmiQueryResult(L"SELECT SerialNumber FROM Win32_PhysicalMedia", L"SerialNumber");
    char* ret = NULL;
    std::string strValue;
    char* strReturn = NULL;
    for (const auto& item : res.ResultList) {
          strValue.assign(item.begin(), item.end());
          ULONG ulSize = strlen(MyString) + sizeof(char);
          strReturn = (char*)::CoTaskMemAlloc(ulSize);
          strcpy(strReturn, strValue.c_str());
          break;
    }
    return strReturn;
}

The dll function calls it from an application written in C#:

Code:

[DllImport("mydll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern string GetSerialHD();

...
string GetHD = GetSerialHD();

function GetSerialHD() always returns null, but the same code in a console application C++ for testing works well.

thanks

Viewing all articles
Browse latest Browse all 3042

Trending Articles