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);
}
}