Click here to Skip to main content
15,886,258 members
Articles / Multimedia / GDI

Interoperability returning a string from a Win32 DLL

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
27 Aug 2013CPOL 11.4K   2  
Returning a string from a Win32 DLL using Interop.

Introduction

Define  a function in native DLL (let's call it “Win32Native.dll”)  as shown below.

C++
extern "C" __declspec(dllexport)  char * GetStringFromDLL() 
{ 
    const size_t alloc_size = 128; 
    STRSAFE_LPSTR result=(STRSAFE_LPSTR)CoTaskMemAlloc(alloc_size); 
    STRSAFE_LPCSTR teststr = "This is return string From Native DLL"; 
    StringCchCopyA ( result, alloc_size, teststr ); 
    return result; 
}

Points of Interest

  • STRSAFE_LPSTR is a typedef of char *
  • StringCchCopy is a replacement for strcpy (with safety).  The size, in characters, of the destination buffer is provided to the function to ensure that StringCchCopyb does not write past the end of this buffer. has two variants.

Writing the client code (the managed part)

We can simply create a console base application which can use this DLL. Let’s name it MarshallingTest.

See the code snippet below.

C#
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.InteropServices; 
using System.Text;
namespace MarshallingTest 
{ 
    class Program 
    { 
       [DllImport("Win32Native.dll")] public static extern String GetStringFromDLL();
        static void Main(string[] args) 
        { 
            Console.WriteLine("Line displayed below is returned from a Native DLL"); 
            string strData = GetStringFromDLL(); 
            Console.WriteLine ( "Returned value [" + strData +"]" ); 
        } 
    } 
}

Points of Interest

  • namespace System.Runtime.InteropServices; defines the declarations necessary for Interop operations, like DllImport.
  • DllImport defines the DLL entry point.

Compile and execute you will get following output.

image

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
More than 10 years of experience in designing and development of GUIs and Middleware for industrial control systems.

Comments and Discussions

 
-- There are no messages in this forum --