#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Location
    South Africa
    Posts
    59
    Rep Power
    11

    Using 32Bit Library in 32Bit Visual C#


    Hello,

    I have a 32bit Delphi Coded DLL File (Simple DLL Binary - not COM / OCX). This DLL exposes certain methods for use. It does not depend on Sharemem libraries of some kind of runtime environment (besides native windows DLLs).

    My Visual Studio is VS Express 2008 (.Net 3.5)

    I have the following code that tries to import thei Library into a C# Console application (console for simplicity only).

    The Delphi declared / exposed function has the following declaration

    Procedure MyProc(aVal: Integer); stdcall;

    As you can see, it takes an Integer value and will simply show a message containing the received value.

    Herewith my C# code

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    
    namespace ConsoleApplication1
    {       
        class Program
        {
           
            [DllImport("MyDLL.DLL", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "MyProc")]
            public static extern void MyProc(int aVar);
    
            static void Main(string[] args)
            {
                string path = Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
                path += ";c:\\iobox\\csharp";
    
                Environment.SetEnvironmentVariable("PATH", path);
                MyProc(5);
                System.Console.Write("a");            
            }
        }
    }
    I Get the following Error

    Code:
    An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
    Any help would be greatly appreciated.

    PS: I understand 64bit processes cannot call 32bit processes - but this doesnt seem like the problem. And the section on Environment Variables was part of problem solving.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Location
    South Africa
    Posts
    59
    Rep Power
    11
    It seems I was mistaken. I downloading the .NET SDK and used CorFlags to force my EXE to 32bit - and it all started to work :-)
    Hence 2008 Express Edition default compiles to 64bit and has no setting to change it - thus I use corflags on the binary after compiling.

IMN logo majestic logo threadwatch logo seochat tools logo