- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个在本地运行的 c# 应用程序,我有一个用 c# 编写的 .dll。
我的应用程序包含
namespace MyApplication
{
public interface IMyInterface
{
IMyInterface Instance { get; }
int MyProp { get; }
}
class MyClass : IMyInterface
{
public int MyProp { get; private set; }
private MyClass instance
public static IMyInterface
{
get
{
if (instance == null)
{
instance = new MyClass();
}
return instance;
}
}
private MyClass() { MyProp = 1; }
}
}
我的图书馆:
namespace MyLibrary
{
public LibClass
{
public static int GetProp()
{
//I want to get the running instance of MyApplication
//If MyApplication is not running I want to start a new instance
//From my application instance I want to
//return the value from MyInterface.Instance.MyProp
}
}
}
一些谷歌搜索让我查看了某种 COM 服务器,但不清楚这是否是最佳方法。我什至不知道要用谷歌搜索什么。最终 MyInterface
会变得复杂很多,并且会包含通知 MyLibrary
刷新数据的事件。实现这一目标的最佳方法是什么?它是 COM 服务器吗?我想在 MyLibrary
使用的 MyApplication
中创建某种 API 吗?
附加信息:
我的 .dll 正在创建为 excel 的实时数据服务器。我希望能够从 excel 访问我的应用程序中的数据并通知 excel 刷新。我不希望我的应用程序有多个实例,因为用户输入将决定 excel 中显示的值。我能够创建 rtd 服务器,但是我不确定访问外部数据的最佳方式是什么。
编辑:
在做了更多研究之后,我想我对在 MyLibrary
中使用 GetActiveObject("MyApplication.IMyInterface")
很感兴趣
namespace MyLibrary
{
public LibClass
{
public static int GetProp()
{
running_obj = System.Runtime.InteropServices.Marshal.GetActiveObject("MyApplication.IMyInterface")
return ((IMyInterface) running_obj).MyProp;
}
private object running_obj = null;
}
}
但我不确定如何在 ROT 中注册 MyApplication.MyClass
。代码原样抛出异常
Invalid class string (Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING))
最佳答案
MyApplication
中添加 public event ComEvent MyApplicationClose;
以在关闭时调用。我能够使用 Managed Event Sinks 使事件正常工作. 编辑反射(reflect)在下面的代码中。
namespace ole32
{
public class Ole32
{
[DllImport( "Ole32.Dll" )]
public static extern int CreateBindCtx( int reserved, out IBindCtx
bindCtx );
[DllImport( "oleaut32.dll" )]
public static extern int RegisterActiveObject( [MarshalAs( UnmanagedType.IUnknown )] object punk,
ref Guid rclsid, uint dwFlags, out int pdwRegister );
[DllImport( "ole32.dll", EntryPoint = "GetRunningObjectTable" )]
public static extern int GetRunningObjectTable( int reserved, out IRunningObjectTable ROT );
[DllImport( "ole32.dll", EntryPoint = "CreateItemMoniker" )]
public static extern int CreateItemMoniker( byte[] lpszDelim, byte[] lpszItem, out IMoniker ppmk );
/// <summary>
/// Get a snapshot of the running object table (ROT).
/// </summary>
/// <returns>A hashtable mapping the name of the object
// in the ROT to the corresponding object</returns>
public static Hashtable GetRunningObjectTable()
{
Hashtable result = new Hashtable();
IntPtr numFetched = IntPtr.Zero;
IRunningObjectTable runningObjectTable;
IEnumMoniker monikerEnumerator;
IMoniker[] monikers = new IMoniker[1];
GetRunningObjectTable( 0, out runningObjectTable );
runningObjectTable.EnumRunning( out monikerEnumerator );
monikerEnumerator.Reset();
while ( monikerEnumerator.Next( 1, monikers, numFetched ) == 0 )
{
IBindCtx ctx;
CreateBindCtx( 0, out ctx );
string runningObjectName;
monikers[0].GetDisplayName( ctx, null, out runningObjectName );
object runningObjectVal;
runningObjectTable.GetObject( monikers[0], out runningObjectVal );
result[runningObjectName] = runningObjectVal;
}
return result;
}
}
}
我的应用程序将充当数据服务器。它接收和处理来自多个来源的数据。对这些数据的访问是通过 COM 公开的。只有一个 MyApplication 实例减少了与外部数据源的连接和处理的冗余,同时允许多个客户端使用它获取的数据。
namespace MyNamespace
{
[ComVisible( true ),
GuidAttribute( "14C09983-FA4B-44e2-9910-6461728F7883" ),
InterfaceType( ComInterfaceType.InterfaceIsDual )]
public interface ICOMApplication
{
[DispId(1)]
int GetVal();
}
//Events for my com interface. Must be IDispatch
[Guid( "E00FA736-8C24-467a-BEA0-F0AC8E528207" ),
InterfaceType( ComInterfaceType.InterfaceIsIDispatch ),
ComVisible( true )]
public interface ICOMEvents
{
[DispId( 1 )]
void ComAppClose( string s );
}
public delegate void ComEvent( string p );
[ComVisible(true)]
[Guid( "ECE6FD4C-52FD-4D72-9668-1F3696D9A99E" )]
[ComSourceInterfaces( typeof( ICOMWnEvents) )]
[ClassInterface( ClassInterfaceType.None )]
public class MyApplication : ICOMApplication, IDisposable
{
//ICOMEvent
public event ComEvent ComAppClose;
protected MyApplication ()
{
//check if application is registered.
//if not registered then register the application
if (GetApiInstance() == null)
{
Register_COMI();
}
}
// UCOMI-Version to register in the ROT
protected void Register_COMI()
{
int errorcode;
IRunningObjectTable rot;
IMoniker moniker;
int register;
errorcode = Ole32.GetRunningObjectTable( 0, out rot );
Marshal.ThrowExceptionForHR( errorcode );
errorcode = BuildMoniker( out moniker );
Marshal.ThrowExceptionForHR( errorcode );
register = rot.Register( 0, this, moniker );
}
public void Dispose()
{
Close( 0 ); //close and clean up
}
//Will look for an existing instance in the ROT and return it
public static ICOMApplication GetApiInstance()
{
Hashtable runningObjects = Ole32.GetRunningObjectTable();
IDictionaryEnumerator rotEnumerator = runningObjects.GetEnumerator();
while ( rotEnumerator.MoveNext() )
{
string candidateName = (string) rotEnumerator.Key;
if ( !candidateName.Equals( "!MyNamespace.ICOMApplication" ) )
continue;
ICOMApplication wbapi = (ICOMApplication ) rotEnumerator.Value;
if ( wbapi != null )
return wbapi;
//TODO: Start the application so it can be added to com and retrieved for use
}
return null;
}
//Builds the moniker used to register and look up the application in the ROT
private static int BuildMoniker( out IMoniker moniker )
{
UnicodeEncoding enc = new UnicodeEncoding();
string delimname = "!";
byte[] del = enc.GetBytes( delimname );
string itemname = "MyNamespace.ICOMApplication";
byte[] item = enc.GetBytes( itemname );
return Ole32.CreateItemMoniker( del, item, out moniker );
}
protected void Close( int i )
{
//Deregistering from ROT should be automatic
//Additional cleanup
if (ComAppClose != null) ComAppClose("");
}
~MyApplication()
{
Dispose();
}
//implement ICOMApplication interface
private static int i = 0;
public int GetVal()
{
return i++; //test value to return
}
}
}
注意:这包含一个用于注册程序集的构建后事件
C:\Windows\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe $(TargetFileName) /codebase /tlb:$(TargetName)TypeLib.tlb
由于它是通过 COM 实现的,因此它应该适用于任何 COM 语言,但我只尝试过 c#。最初,此代码将放入 RTDserver for excel。这将允许用户构建复杂的工作表,利用来 self 的应用程序的实时数据。
首先,我在点网中为我的 COM 对象创建了一个包装器。
namespace COMTest
{
//extend both the com app and its events and use the event sink to get the events
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public sealed class MyAppDotNetWrapper, ICOMEvents, ICOMApplication
{
private ICOMApplication comapp;
public MyAppDotNetWrapper()
{
StartEventSink()
}
//Manage the sink events
private void StartEventSink()
{
//get the instance of the app;
comapp = MyApplication .GetApiInstance();
if (comapp != null)
{
serverconnected = true;
//Start the event sink
IConnectionPointContainer connectionPointContainer = (IConnectionPointContainer) comapp;
Guid comappEventsInterfaceId = typeof (ICOMApplicationEvents).GUID;
connectionPointContainer.FindConnectionPoint(ref comappEventsInterfaceId, out connectionPoint);
connectionPoint.Advise(this, out cookie);
}
}
private void StopEventSink()
{
if (serverconnected)
{
//unhook the event sink
connectionPoint.Unadvise(cookie);
connectionPoint = null;
}
}
//Implement ICOMApplication methods
public int GetVal()
{
return comapp.GetVal();
}
//receive sink events and forward
public event ComEvent ComAppCloseEvent;
public void ComAppClose(string s)
{
serverconnected = false;
ComAppCloseEvent(s);
}
private ICOMApplication comapp;
IConnectionPoint connectionPoint;
private int cookie;
private bool serverconnected;
}
}
现在我可以在我的 .net 应用程序中使用我的包装器
namespace COMTest
{
class Program
{
private static MyAppDotNetWrapper app;
static void Main( string[] args )
{
//create a new instance of the wrapper
app = new MyAppDotNetWrapper();
//Add the onclose event handler
app.ComAppCloseEvent += OnAppClose;
//call my com interface method
Console.WriteLine("Val = " + app.GetVal().ToString());
Console.WriteLine("Val = " + app.GetVal().ToString());
string s = Console.ReadLine();
}
static voic OnClose(string s)
{
Console.WriteLine("Com Application Closed.");
}
}
}
输出在哪里
Val = 1
Val = 2
关闭 COM 服务器应用程序后,您会看到关闭消息。
Val = 1
Val = 2
Com Application Closed.
Running Object Table: Provider in .NET, consumer in MFC
Automating a specific instance of Visual Studio .NET using C#
关于c# - 我可以从我的外部 .dll 访问我的 c# 对象吗?也许使用 ROT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16287563/
如何指示 webpack 排除所有 d3 模块? // does not work externals: { "d3-*": "d3" } 所以如果应用导入了d3-submod
这个问题在这里已经有了答案: 9年前关闭。 Possible Duplicate: What does “outer =>” really mean? 我在哪里可以找到有关信息 trait After
这是一个简单的循环,我正在尝试对性能进行基准测试。 var extremeLoop=function(n){ var time=new Date() var t=0; for(
问题+概述 下面是两个片段,其中包含最初隐藏的 div,然后通过单击 button 和 jQuery 的 .show() 显示。两个 div 都具有由外部样式表应用的 display: grid; 样
我有一个 HTML 页面和一个单独的 .js 文件,该文件包含在带有 的页面中标签。 这是我的 .js 文件: element = document.getElementById("test");
我在 linux 静态库项目中有 3 个文件,我想在两个类方法实现文件中使用的静态字段存在链接问题。我有 class1.h、class1main.cpp 和 class1utils.cpp。 clas
我正在尝试将颜色背景更改为默认背景颜色,当我点击输入框外 我尝试使用“null”或“none”但没有用? window.addEventListener('click', outsideClick);
我正在编写一个应用程序,要求用户在手机上选择各种类型的文件。我使用此代码启动文件选择器 Intent : Intent intent = new Intent(Intent.ACTION_GET_C
在 android 中,不可移动(内部)的外部存储和内部存储有什么区别?我不确定在哪里保存我的数据。我只需要保存一个人可以随时提取的游戏统计数据 谢谢 最佳答案 在许多较新的设备中,将不再有物理区别,
在 C++ 中,假设我们有这个头文件: myglobals.h #ifndef my_globals_h #define my_globals_h int monthsInYear = 12; #en
我正在尝试使用 externs 在 C++ 中连接到 Ada。这两种实现有什么区别? 实现A namespace Ada { extern "C" { int getN
这个问题在这里已经有了答案: Get selected element's outer HTML (30 个答案) 关闭 2 年前。 想象一下我们有这样的东西: Hello World 如果我们这样
假设我在模块的顶部有这个: Public Declare Function getCustomerDetails Lib "CustomerFunctions" () As Long 如果我从 VB6
我目前正在使用这段代码: var wordRandomizer = { run: function (targetElem) { var markup = this.creat
我们正在使用 SVN 试水,并以 Beanstalk 作为主机。我们的设置如下所示: 存储库:模块 模块一 模块二 模块 3 存储库:网站1 自定义网站代码 svn:对模块 1 的外部引用 svn:对
有没有办法在负载均衡器中设置自动外部 IP 分配给像谷歌这样的服务? 我在裸机上运行 Kubernetes。 谢谢 最佳答案 使用 nodePort 类型的服务,它会将您的服务绑定(bind)到所有节
是否有可能在 Controller 之外使用 generateUrl() 方法? 我尝试在带有 $this->get('router') 的自定义存储库类中使用它,但它没有用。 更新 我在这里找到了一
我目前正在尝试通过 Webpack 外部对象外部化 Angular 依赖项来缩短构建时间。到目前为止,我已经为 React 和其他小库实现了这一目标。 如果我只是移动 '@angular/compil
我想创建一个自动应用其他插件的插件(外部插件)。这要求在我称为“应用插件”之前为插件设置构建脚本依赖项。但是似乎我无法在插件中添加buildscript依赖项,或者得到了: 您不能更改处于未解析状态的
我是R包的创建者EnvStats . 有一个我经常使用的函数,叫做 stripChart .我刚开始学习ggplot2 ,并在过去几天里仔细研究了 Hadley 的书、Winston 的书、Stack
我是一名优秀的程序员,十分优秀!