I had my first work on writing a DLL in Delphi. So far, so good. Using typelib, I was able to easily pass Widestrings to and from the DLL.
At the moment, it is curious that I use VB6 as a test bench, and every time I run a test in the IDE, the program starts and then the IDE process suddenly disappears from memory - there are no error messages. If I go through the code, everything will work fine until I get the last line, then the IDE will disappear.
In contrast, when I compile a test in EXE, the program ends without error messages, etc.
Has anyone had this problem before and is there an obvious solution that looks in my face?
The source code is below, in case it matters:
- project
library BOSLAD; uses ShareMem, SysUtils, Classes, BOSLADCode in 'BOSLADCode.pas'; exports version, DMesg, foo; {$R *.res} begin end.
- unit
unit BOSLADCode; interface function version() : Double; stdcall; procedure DMesg(sText : WideString; sHead : WideString ); stdcall; function foo() : PWideString; stdcall; implementation uses Windows; function version() : Double; var s : String; begin result := 0.001; end; procedure DMesg( sText : WideString; sHead : WideString); begin Windows.MessageBoxW(0, PWideChar(sText), PWideChar(sHead), 0); end; function foo() : PWideString; var s : WideString; begin s := 'My dog' got fleas'; result := PWideString(s); end; end.
- typelib
// This is the type library for BOSLAD.dll [ // Use GUIDGEN.EXE to create the UUID that uniquely identifies // this library on the user system. NOTE: This must be done!! uuid(0C55D7DA-0840-40c0-B77C-DC72BE9D109E), // This helpstring defines how the library will appear in the // References dialog of VB. helpstring("BOSLAD TypeLib"), // Assume standard English locale. lcid(0x0409), // Assign a version number to keep track of changes. version(1.0) ] library BOSLAD { // Now define the module that will "declare" your C functions. [ helpstring("Functions in BOSLAD.DLL"), version(1.0), // Give the name of your DLL here. dllname("BOSLAD.dll") ] module BOSLADFunctions { [helpstring("version"), entry("version")] void __stdcall version( [out,retval] double* res ); [helpstring("DMesg"), entry("DMesg")] void __stdcall DMesg( [in] BSTR msg, [in] BSTR head ); [helpstring("foo"), entry("foo")] void __stdcall foo( [out,retval] BSTR* msg ); } // End of Module }; // End of Library
I moved the WideString declaration beyond the function in which I declared it, in the expectation that this would increase the lifetime of the variable longer than just the lifetime of the foo function. It didn't make any difference.
Similarly, I commented on the VB6 call to the foo function. It didn't matter either. No matter what I do, the VB6 IDE dies after the last line of code is executed.
Something other than pointers to local variables is the cause. But what?