简体   繁体   中英

C# P/Invoke embedded VB6 DLL resource: ActiveXObject “Unable to find an entry point named 'FunctionName' in DLL 'DLLName'.”

I'm trying to create a C# wrapper DLL for a VB6 DLL, then use that wrapper in a web page as an ActiveXObject, but I'm getting this error when calling ClassTesting():

Unable to find an entry point named 'ClassTest' in DLL 'VB6DLL'.

The application exports the DLL to a temp directory, then loads it into memory. The structure of the DLL can be described as:

VB6DLL.dll -> public class "VB6.cls" -> public function "ClassTest()".

The C# code is as follows:

namespace SystemDeviceDriver
{
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IDeviceDriver
    {
        [DispId(1)]
        string ClassTesting();
    }

    [Guid("655EE123-0996-4c70-B6BD-7CA8849799C7")]
    [ComSourceInterfaces(typeof(IDeviceDriver))]
    public class DeviceDriver : IDeviceDriver
    {

        [DllImport("kernel32", CharSet = CharSet.Unicode)]
        static extern IntPtr LoadLibrary(string lpFileName);

        [DllImport("VB6DLL", CharSet = CharSet.Unicode)]
        static extern string ClassTest();

        public DeviceDriver()
        {
            //Write the VB6DLL to a temp directory
            string dirName = Path.Combine(Path.GetTempPath(), "SystemDeviceDriver." + Assembly.GetExecutingAssembly().GetName().Version.ToString());
            if (!Directory.Exists(dirName))
            {
                Directory.CreateDirectory(dirName);
            }
            string dllPath = Path.Combine(dirName, "VB6DLL.dll");
            File.WriteAllBytes(dllPath, SystemDeviceDriver.Properties.Resources.VB6DLL);

            //Load the library into memory
            IntPtr h = LoadLibrary(dllPath);
            Debug.Assert(h != IntPtr.Zero, "Unable to load library " + dllPath);
        }

        public string ClassTesting()
        {
            return ClassTest();
        }
    }
}

DllImport / P/Invoke functions are for including "old C style" dll files, so plain simple functions which are exported from a library

here are the calling methods listed, for which function types that is possible: http://msdn.microsoft.com/de-de/library/system.runtime.interopservices.callingconvention.aspx

COM is completely different, see: http://en.wikipedia.org/wiki/Component_Object_Model

the only functions a COM dll typically export are DllRegisterServer, DllUnregisterServer you could first use the P/Invoke functions to call that function. the COM dll file registers itself in the registry. then it should be possible to create a COM object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM