Hello. I have a problem and I would love it if I could get some insight on it.
I have read the forums with many answers about the subject, but I'm still having problems so I'm writing here. Basically I was following this guide which is for creating Managed Wrapper from a static library LIB. I thought, static, dynamic, how different could it be.

I have a C program that allows me to do some functions, for simplicity, I am now only using a simple addition function; but the real thing I ultimately want to do is far more complex. For now I just want this simple thing to work.

I'm using MS Visual Studio 2010. I created a Blank Solution with the following three projects:

  • AdditionDLL
  • Wrapper
  • MainCode

AdditionDLL project is a simple C program that has only one function which is to add two numbers together. It is exported as DLL. The code is as follows:

// AdditionClass.c
#include "AdditionClass"
#include <stdio.h>

#pragma once
double Add(double x, double y){
	return x+y;
}
// AdditionClass.h
#pragma once 
static double Add(double x, double y);

This compiles fine and the DLL is created.

Now the Wrapper project is a managed C++/CLR application that is supposed to use the Add function from the AdditionDLL.dll. So I created the project as C++/CLR and change the properties to depend on the dll and also for the linker. Moreover, I have added the AdditionDLL as a reference. Here is the content of the files:

//ClrWrapper.cpp
#include "stdafx.h"
#include "ClrWrapper.h"

using namespace ClrWrapper;

extern "C" __declspec(dllimport)double Add(double x, double y);
//ClrWrapper.h
using namespace System;

namespace ClrWrapper{
	public ref class AdditionClass{
	public:
		double Add(double x, double y);
	};
}

This doesn't compile. It gives me error LNK1107 invalid or corrupt file cannot read at 0x2F0
I know I have a lot of mistakes because I'm trying to make things communicate which shouldn't be communicating. I'd appreciate any help.


The last project is just the C# code to use the Wrapper. I haven't worked on that yet because my wrapper still doesn't work.

This works. First you have to export the functions in the DLL that you want to expose to the application program. Here I defined a macro named ADDITIONCLASSAPI so that the same *.h file can be used in both the DLL and application program without change. VC++ 2010 (and earlier versions) define _USRDLL when compiling a DLL project,

// AdditionClass.h
#pragma once 
#ifdef _USRDLL
#define  ADDITIONCLASSAPI __declspec(dllexport)
#else
#define ADDITIONCLASSAPI __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C" {
#endif
ADDITIONCLASSAPI double Add(double x, double y);
#ifdef __cplusplus
}
#endif
// AdditionDLL.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"

// AdditionClass.c
#include "AdditionClass.H"
#include <stdio.h>

#pragma once
#ifdef __cplusplus
extern "C" {
#endif
ADDITIONCLASSAPI double Add(double x, double y){
	return x+y;
}
#ifdef __cplusplus
}
#endif

Then the C++/CLI program. Make sure this program links with AdditionClass.lib

#include "stdafx.h"
#pragma comment(lib,"AdditionDLL.lib")
using namespace System;
#include "AdditionClass.h"
namespace ClrWrapper{
	public ref class AdditionClass{
	public:
		double Add(double x, double y) { return ::Add(x,y);}
	};
}
int main(array<System::String ^> ^args)
{
	ClrWrapper::AdditionClass w;
	double n = w.Add(1.0, 2.0);
    Console::WriteLine(n);
	Console::Read();
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.