Where shall I put a DLL generated in another project from the same solution -I am using Visual Basic Visual Studio 2017-?


I have created a C++ DLL in Visual Studio 2017 using the following sources:

Header: stdfax.h

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently

#pragma once

#include "targetver.h"

#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
// Windows Header Files
#include <windows.h>

// reference additional headers your program requires here

Header: targetver.h

#pragma once

// Including SDKDDKVer.h defines the highest available Windows platform.

// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.

#include <SDKDDKVer.h>

Source: stdafx.cpp

#include "stdafx.h"

Source: dll.cpp

// dll.cpp : Defines the exported functions for the DLL application.

#include "stdafx.h"

Source: dllmain.cpp

// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"

                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
    switch (ul_reason_for_call)
    return TRUE;

Source: Source.cpp <- this is where I intend to add my source file

#include "stdafx.h"


int HelloWorld(int A) {
    return (2 * A);

Definition: Source.def <- Intentend to be able to consume this DLL from VB .NET


DESCRIPTION 'A C++ dll tat can be called from VB'


Now I try to consume this DLL using a VB.NET project in the same solution:

Imports System.Runtime.InteropServices

Public Class Form1

    <DllImport("dll.dll", CallingConvention:=CallingConvention.Cdecl)>
    Private Shared Function HelloWorld(ByVal x As Int64) As Int64
    End Function

    Private Declare Function HelloWorld Lib "dll.dll" (int) As Integer

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim j

        j = HelloWorld(CInt(TextBox1.Text))
        TextBox2.Text = j

    End Sub
End Class

But I get:

System.DllNotFoundException: 'Unable to load DLL 'dll.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)'

EDIT: Following Vincent's comment about the possibility of this being a duplicate of Trouble adding my Visual-C++ DLL to my VB.NET windows forms GUI app I have checked that link and it does effectively tells how to add a DLL to a VB project. In that sense it is a duplicate, however, I have fixed that I find now the following error while trying to access the DLL:

System.BadImageFormatException: 'An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)'

I have also, following the side comment, modified my source file containing the function that needs to be exported to:

Source: Source.cpp <- this is where I intend to add my source file

#include "stdafx.h"


extern "C" int HelloWorld(int A) {
    return (2 * A);

I think that fixing that last issue, the question could be useful to others as it is a simple but documented "HelloWorld" example on how to build a DLL in C++ and consume it in VB.NET.

asked on Stack Overflow Oct 7, 2019 by M.E. • edited Oct 8, 2019 by M.E.

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0