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
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; }
相关文章推荐
- C++ 如何引用DLL
- C#引用C++ Dll 所有类型转换方式
- C++ 创建引用DLL
- paip.c++ qt 外部dll共享库的导入以及引用
- C#引用C++ Dll 所有類型轉換的方式
- c#调用C++ dll 报未将对象引用到设置对象的实例 的解决方案
- Unity3D关于内存优化,内存泄露、多线程、引用C++的dll的问题研究
- .net项目引用C++ 动态链接库.dll
- c#引用c++ dll
- SWIG c# 引用 c++dll
- Swig转c++ lib项目到c#可引用的dll
- C#引用C++开发的DLL
- C++ 引用外部 DLL
- C#引用C++的DLL时C#和C++的数据类型对照表
- C#引用C++ Dll 所有类型转换的方式
- C# vb.net 分别引用C++编译DLL
- C#引用C++ Dll 所有类型转换方法
- C++ win32 dll 引用外部CLR,加载托管程序集异常-Error 10 error LNK2019: unresolved external symbol _CLRCreateInstancet
- C#引用C++开发的DLL
- C#引用C++的DLL时C#和C++的数据类型对照表