Page 1 of 1

calling a cobra .dll from excel

PostPosted: Sun Mar 10, 2013 7:32 pm
by DelphiGuy
The Cobra Programming Language svn:2948 (post 0.9.3) / 2013-03-07 on .NET CLR v4.0.30319 on Microsoft Windows NT 6.1.7601 Service Pack 1 at C:\Cobra\Cobra-svn-2948\bin\cobra.exe , and MD 3.0.6 .

how does one call a cobra .dll method from the excel (2002, in this case) VBA editor, and hopefully from an excel spreadsheet as a user-defined function, as well?

well, i started with this promising-looking guide:

http://richnewman.wordpress.com/2007/08/25/a-beginner%E2%80%99s-guide-to-calling-a-net-library-from-access/

i translated his simple C# example into cobra:

Code: Select all
namespace DotNetLibrary

   class DotNetClass
   
      def dotNetMethod(input as String) as String
         return "Hello " + input

i ignored his section about using VS to automagically register the resulting assembly, figuring that MD almost certainly would not have anything equivalent, and instead went to the "Deployment" section to learn how to do it manually with regasm.

i didn't use a "strong name" for my cobra assembly. it's simply named "cobraexceltest.dll". arbitrarily, i put it on the desktop, figuring that i'd sort out any path issues with excel later on.

then, after regasm complained that it couldn't find cobra.core.dll, i put cobra.core.dll on the desktop as well. (i didn't really understand why regasm couldn't find cobra.core.dll, since it's already "registered".)

then, regasm made a new complaint, as follows (from the Win SDK command prompt):

C:\Windows\system32>gacutil /l cobraexceltest
Microsoft (R) .NET Global Assembly Cache Utility. Version 4.0.30319.1
Copyright (c) Microsoft Corporation. All rights reserved.

The Global Assembly Cache contains the following assemblies:

Number of items = 0

C:\Windows\system32>gacutil /l cobraexceltest.dll
Microsoft (R) .NET Global Assembly Cache Utility. Version 4.0.30319.1
Copyright (c) Microsoft Corporation. All rights reserved.

The Global Assembly Cache contains the following assemblies:

Number of items = 0

C:\Windows\system32>regasm /codebase c:\users\paul\desktop\cobraexceltest.dll
Microsoft .NET Framework Assembly Registration Utility version 4.0.30319.17929
for Microsoft .NET Framework version 4.0.30319.17929
Copyright (C) Microsoft Corporation. All rights reserved.

RegAsm : warning RA0000 : Registering an unsigned assembly with /codebase can ca
use your assembly to interfere with other applications that may be installed on
the same computer. The /codebase switch is intended to be used only with signed
assemblies. Please give your assembly a strong name and re-register it.
RegAsm : error RA0000 : A BadImageFormatException has been thrown while parsing
the signature. This is likely due to lack of a generic context. Ensure genericTy
peArguments and genericMethodArguments are provided and contain enough context.

C:\Windows\system32>


the article did predict that regasm would warn me that it wants a strong name, but what confused me was the "lack of a generic context" error. i have a hunch, but not for any good reason, that this could be related to 64-bit versus 32-bit. but that's just a hunch.

any help on understanding what this means, and what to do about it, very welcome. thanks.

Re: calling a cobra .dll from excel

PostPosted: Mon Mar 11, 2013 7:53 pm
by DelphiGuy
since i'm not getting anywhere with my ability to manually register a cobra assembly as a com object using regasm, i'm switching to Plan B: i'm using MSVS Express 2012 to create the article's sample assembly in C#, and sure enough it is magically registered and runs beautifully from Excel 2002's macro editor.

now to figure out how to use the C# assembly as a wrapper for my cobra assembly. i assume that my C# assembly can probably just inherit a class exposed by my cobra assembly, and that the mechanics of doing so are trivial.

and then to figure out, on the excel end of things, how to have the C#'s methods available, not only to the excel macro editor, but also to the spreadsheet's user-defined functions. there's no shortage of articles alluding to this available on the web.

in the interim, any comments or even rough guidance, very much appreciated.

i note, on an unrelated topic, that C# seems kinda goofy and overly ornate compared to the elegance of cobra.