Forcing 32-bit VB.NET mode on 64-bit machine

bjp1662 used Ask the Experts™
Using my 64-bit machine, I have compiled a Win32 DLL using Visual C++ 2008 Express.  When I try to call a routine from this DLL using VB.NET 2008 Express, I get the error message "An attempt was made to load a program with an incorrect format".  Following advice elsewhere, I used Notepad to change all Platform configurations to "x86" (were previously "Any CPU") in my VB.NET project files.  However, the error message still remains.  The problem is not that the DLL is in the wrong place; when I rename it, VB.NET throws a different error saying it can't find the DLL.  What can I do to get VB.NET 2008 Express to properly execute my DLL on my 64-bit machine?
<DllImport("WebCamLib.dll", EntryPoint:="Initialize")> _
Public Shared Function WebCamInitialize() As Integer
End Function

'This generates the error:
If NativeMethods.WebCamInitialize() <> 0 Then

In C++:
__declspec(dllexport) DWORD APIENTRY Initialize()
	// Nothing to really do yet
	return hr;

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
is C++ dll also 32-bit? you could use CorFlags utility to force it to 32-bit
IT Consultant
Top Expert 2010
Go to Tools -> Options... -> Projects and Solutions -> General -> enable Show advanced build configurations

Go to Build -> Configuration Manager

Create a new solution platform, choose x86.

Make sure the 32-bit DLL is in C:\WINDOWS\SysWOW64 or in the same folder as your VB.Net binary.


The DLL is a Win32 DLL (the Active solution platform is "Win32" in Visual C++).  I am unable to follow tgerbert's suggestion as VB.NET 2008 Express does not have the option to change platforms in the IDE.  However, as described in my original post, I have already set all platform configuration variables from "Any CPU" to "x86" in my VB.NET solution and project using a text editor; this does not fix the problem.

As a further data point, I copied the project directory to a 32-bit machine and it executed properly without modification.
Todd GerbertIT Consultant
Top Expert 2010

Those screen shots I posted are from VB.Net 2008 Express, so I would disagree with your assertion that it is not possible, using the IDE, to change the target platform (in the second image you can even read "Visual Basic 2008 Express Edition" in the title bar).  :)


Ha, you're right; my bad :)

As it turns out, when I replaced "Any CPU" with "x86" in the Solution and Project files using a text editor, it changed the value that appears in the Active solution platform drop down, but it didn't actually change the project to x86.  Starting a new project and following your instructions produced the desired result.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial