lightdoll said:
Hello...Everyone...
i want to make a singleton dll or exe using C#,
i want to call the C# dll or exe from C, C++, VB...
The C# dll will be called by any process(C, C++, VB), after any process
call the C# dll, the C# dll have a same instance all of any process , like
a
ATL Singleton(Out-of-Service), so i can use a shared data from C# dll
Could you tell me how i can make this kind of dll by C#...
This is exactly what COM+ and the EnterpriseServices are made for.
Derive from ServicedComponent, enable object pooling for the component, set
both the max and min pool size to 1, and enable JITA. Register your C# DLL
as a COM+ server type application and you got a singleton out-proc server,
which you can access from COM (VB6, C, C++, ...) and .NET clients.
The following snippet illustrates how you can declare the required
attributes.
using System.EnterpriseServices;
.....
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: ApplicationName("Singleton")]
[assembly: ApplicationID("xxxxxxxxxxxxxxxxxxxxxxxxxxx")]
[assembly: ApplicationActivation(ActivationOption.Server)]
[assembly: ApplicationAccessControl(Value=false,
Authentication=AuthenticationOption.None)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("wwwwwwwwwwwwwwwww")]
public interface ISingleton
{
void Foo();
....
}
[JustInTimeActivation]
[ObjectPooling(Enabled=true, MinPoolSize=1, MaxPoolSize=1,
CreationTimeout=2000)]
[Guid("zzzzzzzzzzzzzzzzzzzzzzzzzzz")]
[ProgId("SomeOnes.Singleton")]
[ComVisible(true)]
public sealed class Singleton : ServicedComponent, ISingleton
{
public Singleton () {}
public void Foo()
{
....
}
}
Substitute a valid uuid for xxxxxxxxx, wwwwwwwww and zzzzzzzzzzzzzzzzz (run:
uuidgen -x -n3)
Compile and register using regsvcs <yourdll>
Native latebound COM clients can call a method using the ProgId
'VBScript client
set o = CreateObject("SomeOnes.Singleton")
o.Foo()
....
o.Dispose 'when done
Early bound clients (C, VB, C++ etc...) can use the tlb produced by the
registration process,
..NET clients can access the metadata needed by setting a reference to the
assembly containing the interface definition when compiling the client.
csc /r:yourdll singletonclient.cs
and call into the component like this:
....
ISingleton mySingleton = new Singleton();
IDisposable disposableSingleton = mySingleton as IDisposable;
try
{
mySingleton.Foo();
}
finally
{
disposableSingleton.Dispose();
}
Willy.