Using PInvoke to access my C++ lib not working

227 views
Skip to first unread message

Kenneth Chin

unread,
Feb 17, 2015, 11:36:40 AM2/17/15
to exce...@googlegroups.com
Hi,

I have a simple project in MS VS 2013 to test accessing a simple C++ function in Excel, using P/Invoke and Excel-DNA.

I successfully followed this tutorial to get P/Invoke working with a C++ library.

But once I layered on the Excel-DNA to create the XLL Add-In I get this error:

External library could not be registered - Path: CSProject.dll
    Error: Could not load file or assembly 'file:///C:\Users\kchin\Documents\Visual Studio 2013\Projects\PInvokeTest\Debug\CSProject.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Any help is appreciated!

I've attached my whole project.

Here's my C++ 

CPPFunctions.h
#ifndef _CPPLIB_H_
#define _CPPLIB_H_

#ifndef MYAPI
#define MYAPI 
#endif

#ifdef __cplusplus
extern "C" {
#endif

MYAPI int kAdd(int a, int b);

#ifdef __cplusplus
}
#endif

#endif // _CPPLIB_H_

CPPFunctions.cpp
#include "CPPFunctions.h"
#include <stdio.h>

MYAPI int kAdd(int a, int b) {
//printf("%x\n", str);
return a + b;
}

C# Code

PInvokeTest.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using ExcelDna.Integration;

namespace PInvokeTest
{
    class Program
    {
        //static void Main(string[] args)
        //{
        //    Console.WriteLine(kAdd(4, 4));
        //}

        [DllImport("CPPLib.dll"),ExcelFunction(Description="My first Excel-DNA function")]
        public static extern int kAdd(int a, int b);
    }
}
PInvokeTest.zip

Govert van Drimmelen

unread,
Feb 18, 2015, 4:45:46 PM2/18/15
to exce...@googlegroups.com
Hi Kenneth,

You might like to check the following:

* The C# class that defines your (public static) function should be 'public'.
* The C++ project must be compiled for the right bitness for the Excel you're running under. The .xll file in the project you sent was for 64-bit Excel, but the C++ project you sent was 32-bit.

Then it worked fine on my machine (with 32-bit Excel) after doing an "Install-Package Excel-DNA" to install the Excel-DNA NuGet package into the CSProject project.

So basically it should work, and the error is likely some local issue.

Regards,
Govert



From: exce...@googlegroups.com [exce...@googlegroups.com] on behalf of Kenneth Chin [kennet...@gmail.com]
Sent: 17 February 2015 06:36 PM
To: exce...@googlegroups.com
Subject: [ExcelDna] Using PInvoke to access my C++ lib not working

--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To post to this group, send email to exce...@googlegroups.com.
Visit this group at http://groups.google.com/group/exceldna.
For more options, visit https://groups.google.com/d/optout.

Kenneth Chin

unread,
Feb 18, 2015, 10:01:53 PM2/18/15
to exce...@googlegroups.com
Hi Govert,

Thanks for the reply. 

I'm running on Win7 64-bit OS and using Excel 2013 64-bit. I'm unable to test on a 32-bit Excel.

I did some changes and am now stuck on this error in Excel after calling my function:

"A value used in the formula is of the wrong data type."

The changes I made:
  • Made the C# class public
  • In configuration manager, changed the C++ project to x64 by selecting New then Copy settings from Win32

The confusing thing is, after the above changes, I tried changing the C# project to Console Application and Platform target to x86, uncommented out Main to run as an executable off the command line it works. It won't work when I select Any CPU or x64 as the platform target. 
I get the error:
Unhandled Exception: System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
   at PInvokeTest.Program.kAdd(Int32 a, Int32 b)

Are you able to get it running on Excel 64-bit? 

Ken
To post to this group, send email to exc...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages