Hi, this is the way I find to
use unicode strings in a ShowMessage dialog (
as a test only of course, it can be used for anything...) stored inside a
lazarus .dll under Windows 7 called from C#.
This is the DLL compiled with Lazarus (it uses LCL as dependency in order to enable dialog message...):
library csharpdllstring;
{$mode objfpc}{$H+}
uses
Interfaces, Classes, Dialogs
{ you can add units after this };
procedure ShowDefaultMessage(); stdcall;
begin
ShowMessage('Example Message.');
end;
procedure ShowStringMessage(const message: PWideChar); stdcall;
begin
ShowMessage(UTF8Encode(WideString(message)));
end;
exports
ShowDefaultMessage,
ShowStringMessage;
begin
end.
There are two functions, one that will display a message that is stored internally in the dll... just a test.
The most important is the second.
This is the C# code to load the DLL and run the methods from the C# application. Is important to configure the Platform Target to x86 since the dll I've compiled is 32 bit.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace UsingDLL
{
public partial class Form1 : Form
{
// 32 bit dll, the CPU must be set to 32 bit in the project settings
[DllImport("csharpdllstring.dll")]
private static extern void ShowDefaultMessage();
[DllImport("csharpdllstring.dll")]
private static extern void ShowStringMessage([MarshalAs(UnmanagedType.LPWStr)]string message);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ShowDefaultMessage(); // working
ShowStringMessage("Test ñññ string"); // working
}
}
}
There are two ways of returning a string, both requires additional code, one in the dll side, other in the c# side.
https://stackoverflow.com/questions/16170360/returning-a-string-from-delphi-dll-to-c-sharp-caller-in-64-bitSeems that the First approach is hard to write in the dll but is then easier to use in c#. The main problem seems that
CoTaskMemAlloc is only available under windows?
function CoTaskMemAlloc(cb : ULONG) : PVOID; stdcall; external 'ole32.dll' name 'CoTaskMemAlloc';
procedure CoTaskMemFree(pv : PVOID); stdcall; external 'ole32.dll' name 'CoTaskMemFree';