Calling D from C++

Loopback elliott.darfink at gmail.com
Mon Jul 18 19:13:26 PDT 2011


On 2011-07-19 00:56, Johann MacDonagh wrote:
> On 7/18/2011 5:04 PM, Andrej Mitrovic wrote:
>> On 7/18/11, Loopback<elliott.darfink at gmail.com> wrote:
>>> On 2011-07-18 21:59, Andrej Mitrovic wrote:
>>>> import core.dll_helper; is outdated, use import core.sys.windows.dll;
>>>> And also import core.runtime;
>>>
>>> Are there any examples covering these new modules, or are the procedure
>>> the same?
>>>
>>
>> It's all pretty much the same as the page says. There's a DLL example
>> in this folder:
>> \DMD\dmd2\samples\d\mydll
>
> Looks like VisualD's DLL template needs to be updated.
>
> FWIW I was able to compile and link a D DLL with the code I copy pasted
> in the other message.
>
> Loopback, do you have Visual Studio on your dev box? If so you should
> take a look at VisualD: http://www.dsource.org/projects/visuald
>
> Let us know if you're not able to compile a D DLL.

Thanks for all of your replies!

It seems as if the problem was solved by using core.sys.windows.dll
instead. Although I do have a bit of a problem. As I mentioned earlier
I wanted to make a DLL which C++ applications could interface with.

By making this possible, I have a "dll.d" file (just as the example) and
a "mydll.d" file - the file which implements the dll functions. In this
file, I have declared a function like this (testing purpose):

extern(C++) void SetInt(int * foo)
{
	*foo = 5;
}

When I compile this program with this DMD command line:
dmd -ofmydll.dll -L/IMPLIB mydll.d dll.d mydll.def

I get a successful compile, and a DLL file generated. For
some reason, I also get a ATA.lib file, which is exactly
1.00 KB big (any ideas why)?

The problem though, is related to the C++ side. I load the
DLL dynamically using "LoadLibraryA" function, and then
I try to load the SetInt function using GetProcAddress.

The LoadLibrary function succeeds but the GetProcAddress
function call fails saying that the function could not be
found.

typedef void (*PFGMO)(int*);

void main(int argc, char ** argv)
{
	// Function Pointer
	PFGMO pFGMO = NULL;

	HINSTANCE library;
	if((library = LoadLibraryA("C:\\mydll.dll")) == NULL)
		throw 1;

	bool result = (pFGMO = (PFGMO) GetProcAddress(library, "SetInt")) != NULL;

	// Result is zero (failed)
	std::cout << "Result: " << result;
	std::cin.get();
}

How come the procedure address cannot be found and how to solve it?
If of interest, this is the .def file (mydll.def)

LIBRARY "mydll.dll"
EXETYPE NT
SUBSYSTEM WINDOWS
CODE SHARED EXECUTE
DATA WRITE

When viewing the DLL with PE Explorer (program to explore executable
files) it reports that the number of symbols are zero. Is this related
to the problem perhaps?


More information about the Digitalmars-d-learn mailing list