Steps to create dll in C # which can then be used from php

After picking up a bit of information from various posts, I've gotten to this so far, but still not there.

I am trying to create a dll in C # which php can then use.

I created a VS2012 C # Class Library project:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace test1
{
    [System.Runtime.InteropServices.Guid("0283F337-AF8D-45BE-BD32-829F5976E9EF")]
    [ComVisible(true)]
    public class Class1
    {
        public Class1()
        {
            // do nothing
        }

        public int blah()
        {
            return 42;
        }
    }
}

      

Platform target: any processor

Target Framework: .NET Framework 4

I compiled this and got the file test1.dll.

I've tried registering it with:

regsvr32 test1.dll

      

but get the following error:

The module "test1.dll" was loaded but the entry-point DllRegisterServer was not found.

      

Also tried:

regsvr32 /i /n test1.dll

      

but I am getting this error:

The module "test1.dll" was loaded but the entry-point DllInstall was not found.

      

I use:

regasm test1.dll

      

and says he's successful.

Then I create a test php file:

<?php
$obj = new COM("test1.Class1");
$output=$obj->blah();
echo $output;
?>

      

I also add the following to my php.ini:

[PHP_COM_DOTNET]
extension=php_com_dotnet.dll

      

but when i run it i get:

Fatal error: Uncaught exception 'com_exception' with message
'Failed to create COM object `test1.Class1': Class not registered

      

So, it seems to me that I should use regsvr32 and make some changes in C # to solve the DllRegisterServer problem

Can someone fill me in with what I need to do to make this work.

I use:

PHP 5.6.7
Visual Studio Express 2012
Windows 8.1 (64 bit version)

      

Adding:

Also tried:

regasm test1.dll /tlb:test1.tlb
regsvr32 test1.tlb

      

This gives me:

The module "test1.tlb" may not be compatible with the version of Windows
that you're running.  Check if the module is compatible with an x86 (32-bit)
or x64 (64-bit) version of regsvr32.exe.

      

I get this message whether the target platform is "x64" or "any processor".

+3


source to share


2 answers


This isn't really an answer, but it works a bit. 64-bit doesn't seem to work on Windows 8.1 or VS2012 Express or in combination with both, although it is a 64-bit OS.

If I build a solution with an x86 target, everything works fine.

I also found that there are two regsvr32.exe files in Windows 8.1, one in c: \ windows \ system32 and one in c: \ windows \ SysWoW64. I have had no success with any of them.

The first is for registering 64-bit versions of Dlls, and the second is for 32-bit versions.

Luckily for me, I don't need to have 64-bit.

All that said, I can only register it in VS, so I still need to figure out how to register it with the server itself.

Adding

Try to get closer today - you can now register it on my development machine on the command line

Steps used:

  • Run VS2012 as Administrator
  • I removed the line [System.Runtime.InteropServices.Guid ... doesn't seem to be needed
  • I signed the assembly:
    • right click on the project name in solution explorer and select properties
    • Select "Signing"
    • Check "Sign the assembly"
    • Select "New ..." under "Select Strong Named File"
    • I gave my name test1.snk and decided not to password protect it.
  • Build it (F7)
  • From cmd prompt (run as administrator):
    • C: \ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ RegAsm.exe test1.dll / tlb: test1.tlb / codebase

This now works on my Windows 8.1 PC



I'll copy this to a Win 2008 R2 server and try the same:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe test1.dll /tlb:test1.tlb /codebase

      

but I am getting a popup with this error:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe is not a valid Win32 application

      

followed by "Access Denied". at the command line cmd.

I found that on Win8.1 and 10 on Win2008R2 there are 12 different versions of regasm.exe installed. I've tried them all, but the one that works is the one I've been using all along.

In case of use by anyone, here they are:

WIN 2008 R2
C:\Windows\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe
C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe
C:\Windows\Microsoft.NET\Framework64\v2.0.50727\RegAsm.exe
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe
C:\Windows\winsxs\amd64_regasm_b03f5f7f11d50a3a_6.1.7601.17514_none_a3c349b4bdac0898\RegAsm.exe
C:\Windows\winsxs\amd64_regasm_b03f5f7f11d50a3a_6.1.7601.18523_none_a3c462acbdab0575\RegAsm.exe
C:\Windows\winsxs\amd64_regasm_b03f5f7f11d50a3a_6.1.7601.22733_none_8cf7bc12d7517f23\RegAsm.exe
C:\Windows\winsxs\x86_regasm_b03f5f7f11d50a3a_6.1.7601.17514_none_eb70808bd228319e\RegAsm.exe
C:\Windows\winsxs\x86_regasm_b03f5f7f11d50a3a_6.1.7601.18523_none_eb719983d2272e7b\RegAsm.exe
C:\Windows\winsxs\x86_regasm_b03f5f7f11d50a3a_6.1.7601.22733_none_d4a4f2e9ebcda829\RegAsm.exe

WIN 8.1
C:\Windows\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe
C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe
C:\Windows\Microsoft.NET\Framework64\v2.0.50727\RegAsm.exe
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe
C:\Windows\WinSxS\amd64_regasm_b03f5f7f11d50a3a_4.0.9600.16384_none_95117f30a7195a76\RegAsm.exe
C:\Windows\WinSxS\amd64_regasm_b03f5f7f11d50a3a_6.3.9600.16384_none_ea742672c514173d\RegAsm.exe
C:\Windows\WinSxS\amd64_regasm_b03f5f7f11d50a3a_6.3.9600.17226_none_ea6df930c519af4b\RegAsm.exe
C:\Windows\WinSxS\amd64_regasm_b03f5f7f11d50a3a_6.3.9600.20708_none_d39e21d6dec31402\RegAsm.exe
C:\Windows\WinSxS\x86_regasm_b03f5f7f11d50a3a_4.0.9600.16384_none_dcbeb607bb95837c\RegAsm.exe
C:\Windows\WinSxS\x86_regasm_b03f5f7f11d50a3a_6.3.9600.16384_none_32215d49d9904043\RegAsm.exe
C:\Windows\WinSxS\x86_regasm_b03f5f7f11d50a3a_6.3.9600.17226_none_321b3007d995d851\RegAsm.exe
C:\Windows\WinSxS\x86_regasm_b03f5f7f11d50a3a_6.3.9600.20708_none_1b4b58adf33f3d08\RegAsm.exe

      

So I'm one step closer, but still not quite there. Still can't compile 64-bit (not needed but hoped I could get that version to work on WIN2008R2) and can't register 32-bit (x86) on the server.

Addendum2

Finally, success . I noticed that C: \ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ RegAsm.exe was zero bytes in size (goodness knows why), so I copied one from another win2008r2 server and everything is fine. So I'm a lucky person - don't care about the 64 bit version :-)

0


source


test1.dll

is not actually a COM dll, you cannot register it.

You need to follow the steps ( https://msdn.microsoft.com/en-us/library/x66s8zcd.aspx ) to enable COM.

enter image description here

To set up code in your class to create a COM object

In Solution Explorer, double-click Class1.vb to display its code. Rename the class to ComClass1. Add the following constants to ComClass1. They will store the Globally Unique Identifier (GUID) constants that COM objects must have.

VB.NET

Public Const ClassId As String = "" 
Public Const InterfaceId As String = "" 
Public Const EventsId As String = ""

      

On the Tools menu, click Create Index. In the Generate GUID dialog box, select Registry Format and click Copy. Click "Exit".

Replace the empty string for ClassId with GUID, removing the leading and trailing curly braces. For example, if the GUID provided by Guidgen is "{2C8B0AEE-02C9-486e-B809-C780A11530FE}" then your code should look like this.

VB.NET

Public Const ClassId As String = "2C8B0AEE-02C9-486e-B809-C780A11530FE"

      

Repeat the previous steps for the InterfaceId and EventsId constants as in the following example.



VB.NET

Public Const InterfaceId As String = "3D8B5BA4-FB8C-5ff8-8468-11BF6BD5CF91" 
Public Const EventsId As String = "2B691787-6ED7-401e-90A4-B3B9C0360E31"

      

Make sure GUIDs are new and unique; otherwise, your COM component may interfere with other COM components.

Add the ComClass attribute to ComClass1, specifying GUIDs for the class ID, interface ID, and event ID, as in the following example:

VB.NET

<ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)>
Public Class ComClass1

      

COM classes must have a parameterless constructor Public Sub New()

, or the class will not be registered correctly. Add a parameterless constructor to the class:

VB.NET

Public Sub New()
    MyBase.New()
End Sub

      

Add properties, methods, and events to the class by ending it with the End Class statement. Select Build Solution from the Build menu.

Now you can register your dlls with regsvr32

.

+1


source







All Articles