Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ &引用;新关键字“的使用无效”;在VBA中使用c++;_C++_Vba_Com - Fatal编程技术网

C++ &引用;新关键字“的使用无效”;在VBA中使用c++;

C++ &引用;新关键字“的使用无效”;在VBA中使用c++;,c++,vba,com,C++,Vba,Com,我在网上和stackoverflow上搜索了这个答案,但什么也找不到。我在C++中编写了一个COM对象(第一次),它在VBScript中使用,并在可执行文件中通过 COCCREATEAUTION < /代码>。所以我决定看看它是否能在Excel VBA中工作 所以我进入了“参考”并在那里找到了我的对象。选中该框并开始编码。下面是VBA代码 Function doCos(x As Double) As Double Dim t As SimpleLib.IMath Set t = New

我在网上和stackoverflow上搜索了这个答案,但什么也找不到。我在C++中编写了一个COM对象(第一次),它在VBScript中使用,并在可执行文件中通过<代码> COCCREATEAUTION < /代码>。所以我决定看看它是否能在Excel VBA中工作

所以我进入了“参考”并在那里找到了我的对象。选中该框并开始编码。下面是VBA代码

Function doCos(x As Double) As Double
  Dim t As SimpleLib.IMath
  Set t = New SimpleLib.IMath ' <- "Invalid use of New keyword" error here
  doCos = t.Cos(x)
End Function
函数doCos(x为Double)为Double
将t设置为SimpleLib.IMath

Set t=New SimpleLib.IMath'一种方法是在IDL中定义一个包含所需接口的类(在我的例子中是IMath)。注意:默认情况下,
[default]
界面是隐藏的。因此,我只是将
接口IUnknown
定义为默认值。使用MIDL编译后,将生成一个类型库,该类型库应使用regtlibv12.exe注册

然后,我在
DllGetClassObject
中包含了一个额外的IF语句,比如
IF(rclsid==CLSID\u Math)
其中
CLSID\u Math
对应于从MIDL自动生成的文件中定义的CLSID。我所做的只是从
IF(rclsid==IID_IMath)
复制并粘贴IF语句体,更新了DLLRegisterServer和DLLUnRegisterServer函数,重新编译了项目,以及regsvr32.exe

因此,下面的方法现在起作用了

Function docos(x As Double) As Double
  Dim a As SimpleLib.IMath
  Set a = New SimpleLib.Math
  docos = a.Cos(x)
End Function

谢谢汉斯给我关于coclass的提示。学到了一些新的有用的东西。

您是否尝试过使用CreateObject而不是new?您正在尝试创建一个接口而不是对象。当然无效。是的。CreateObject可以工作。但是我想知道如何将其公开给
新的
调用。尝试将其包装到类对象中?我不能,唯一明显的是您的代码是错误的。您的COM服务器中有一个命名的coclass,它就是您要创建实例的coclass。也许它被命名为“数学”,这将是一个合乎逻辑的选择,但这只是一个猜测。