您的位置:首页 > 编程语言 > C语言/C++

C++引用C的dll

2019-03-06 11:14 831 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/tangchuanhui/article/details/88224575

extern "C"介绍:https://www.geek-share.com/detail/2581411700.html

引用dll介绍:https://www.geek-share.com/detail/2581432100.html

隐式引用见https://docs.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=vs-2017

dumpbin查看dll函数:https://www.geek-share.com/detail/2582727621.html

 

显示引用为:

MathLibrary.h

[code]// MathLibrary.h - Contains declarations of math functions
#pragma once

#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif

// The Fibonacci recurrence relation describes a sequence F
// where F(n) is { n = 0, a
//               { n = 1, b
//               { n > 1, F(n-2) + F(n-1)
// for some initial integral values a and b.
// If the sequence is initialized F(0) = 1, F(1) = 1,
// then this relation produces the well-known Fibonacci
// sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

// Initialize a Fibonacci relation sequence
// such that F(0) = a, F(1) = b.
// This function must be called before any other function.
extern "C" MATHLIBRARY_API void fibonacci_init(
const unsigned long long a, const unsigned long long b);

// Produce the next value in the sequence.
// Returns true on success and updates current value and index;
// false on overflow, leaves current value and index unchanged.
extern "C" MATHLIBRARY_API bool fibonacci_next();

// Get the current value in the sequence.
extern "C" MATHLIBRARY_API unsigned long long fibonacci_current();

// Get the position of the current value in the sequence.
extern "C" MATHLIBRARY_API unsigned fibonacci_index();

MathLibrary.cpp

[code]// MathLibrary.cpp : Defines the exported functions for the DLL.
#include "stdafx.h"
#include <utility>
#include <limits.h>
#include "MathLibrary.h"

// DLL internal state variables:
static unsigned long long previous_;  // Previous value, if any
static unsigned long long current_;   // Current sequence value
static unsigned index_;               // Current seq. position

// Initialize a Fibonacci relation sequence
// such that F(0) = a, F(1) = b.
// This function must be called before any other function.
void fibonacci_init(
const unsigned long long a,
const unsigned long long b)
{
index_ = 0;
current_ = a;
previous_ = b; // see special case when initialized
}

// Produce the next value in the sequence.
// Returns true on success, false on overflow.
bool fibonacci_next()
{
// check to see if we'd overflow result or position
if ((ULLONG_MAX - previous_ < current_) ||
(UINT_MAX == index_))
{
return false;
}

// Special case when index == 0, just return b value
if (index_ > 0)
{
// otherwise, calculate next sequence value
previous_ += current_;
}
std::swap(current_, previous_);
++index_;
return true;
}

// Get the current value in the sequence.
unsigned long long fibonacci_current()
{
return current_;
}

// Get the current index position in the sequence.
unsigned fibonacci_index()
{
return index_;
}

把MathLibrary.dll 和 MathClient.cpp放在一起

MathClient.cpp

[code]// MathClient.cpp : Client app for MathLibrary DLL.
#include <stdafx.h>
#include <iostream>
#include <Windows.h>
#include<stdio.h>
//#include "stdafx.h"
using namespace std;

int main()
{
typedef void(*fibonacci_init)(unsigned long long a, unsigned long long b);
typedef long long(*fibonacci_current)();
HMODULE hDLL = LoadLibrary("MathLibrary.dll");

fibonacci_init f_init = (fibonacci_init)(GetProcAddress(hDLL, "fibonacci_init"));
f_init(1, 1);

fibonacci_current f_current = (fibonacci_current)(GetProcAddress(hDLL, "fibonacci_current"));
cout << f_current() << endl;

system("pause");
return 0;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: