最近对一些第三方类库进行c++托管以便c#调用  因为之前没弄过,出现各种各样的问题

fatal error LNK1104: 无法打开文件“xxx.lib”或者xxx.dll 等等等

总结:

1.字符集:设置一样

2.平台:设置一样,比如32位  就都设置32位,这里千万要注意:设置平台的时候要一个个看清楚才行  不然在上面设置了解决方案,下面项目有些没反应(新建的)

3.引用进来的dll还有头文件,如果头文件中引用了其他的文件,记得通通给它导进来了

4.这样还不一定行,还会出现各种未识别,未能解析提示,看你的要引用的头文件是不是需要引用其他的.lib文件  ,都引用进来试试

5.好了,没报错  但是你创建的c#引用c++dll报错了,未能加载dll,那你要看看托管的工程dll和托管要包装的dll有没有复制到c#项目运行目录下了

6.如果还是不行  c#的项目平台是不是都是一样的

7.还是不行,用管理员运行你的vs  或者可以先用管理员运行exe看行不行。

类型转换

c++

// ClassLibrary1.h

#pragma once
#pragma comment (lib,"libthirdparty.lib")
#pragma comment (lib,"libmupdf.lib")
#pragma comment (lib,"MuPDFLib-x64.lib")
#include"..\MuPDFlib\MuPDFClass.h" using namespace System; namespace MuPDFCv { public ref class MuPDFLG
{
// TODO: 在此处添加此类的方法。
public:
MuPDFLG();
int TLoadPdf(String ^ filename, String ^ password);
int TLoadPage(int pageNumber);
int TGetCount();
unsigned char * TGetBitmap(int width, int height, float dpiX, float dpiY, int rotation, int colorspace, bool rotateLandscapePages, bool convertToLetter, int & pnLength, int maxSize);
int Test(int width);
private:
MuPDFClass * m_pMuPDFClass;
};
}
// 这是主 DLL 文件。

#include "stdafx.h"
#include "MuPDFCv.h" using namespace System::Runtime::InteropServices;
//#include <msclr\marshal.h>
//using namespace msclr::interop;
#include <vcclr.h> MuPDFCv::MuPDFLG::MuPDFLG()
{
m_pMuPDFClass = new MuPDFClass();
} int MuPDFCv::MuPDFLG::Test(int width)
{
width++;
return ;
} unsigned char * MuPDFCv::MuPDFLG::TGetBitmap(int width, int height, float dpiX, float dpiY, int rotation, int colorspace, bool rotateLandscapePages, bool convertToLetter, int & pnLength, int maxSize)
{
unsigned char *pData =
m_pMuPDFClass->GetBitmap(width, height, dpiX, dpiY, rotation, colorspace, rotateLandscapePages, convertToLetter, pnLength, maxSize);
return pData;
} int MuPDFCv::MuPDFLG::TLoadPdf(String ^ filename, String ^ password)
{
// 将string转换成C++能识别的指针
/*char* strFilename = marshal_as<char*>(filename);
char* strPassword = marshal_as<char*>(password);*/
char* strFilename = (char*)(void*)Marshal::StringToHGlobalAnsi(filename);
char* strPassword = (char*)(void*)Marshal::StringToHGlobalAnsi(password);
//Ptr
return m_pMuPDFClass->LoadPdf(strFilename, strPassword);
} int MuPDFCv::MuPDFLG::TLoadPage(int pageNumber)
{
return m_pMuPDFClass->LoadPage(pageNumber);
} int MuPDFCv::MuPDFLG::TGetCount()
{
return m_pMuPDFClass->_PageCount;
}

c# 怎么引用的:先直接引用dll

代码:

using MuPDFCv;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DateTime _StartDateTime;
private void button1_Click(object sender, EventArgs e)
{
_StartDateTime = DateTime.Now;
int j;
//"d:\\1.pdf"
string path = @"D:\test\pdf\45x20 number without hologram.pdf";
MuPDFLG mup = new MuPDFLG();
mup.TLoadPdf(path, null);
int count = mup.TGetCount(); mup.TLoadPage(); int h = ; //CPdf.GetHeight();//得到pdf默认高度
int w = ; //CPdf.GetWidth();
//CPdf.SetAlphaBits(4);
//int iLength = 0;//总字节数w*h*(ch+alph)
float iDipX = ;//如果指定了w,不起作用
float iDipY = ;//如果指定了h,不起作用
int rot = ;//-90,90,180
int color = ;//0:rgba 1:ga
byte[] date;
unsafe
{
int iLength = ; byte* intP = mup.TGetBitmap(w, h, iDipX, iDipY, rot, color, false, false, &iLength, ); date = new byte[iLength];
//用下面的赋值多200多毫秒
//for (int i = 0; i < iLength; i++)
//{
// date[i] = *intP;
// intP++;
//}
//用这个用时较少
Marshal.Copy((IntPtr)intP, date, , iLength);
//Marshal.Copy(intP, date, 0, iLength);
}
//mup.TGetBitmap(ref w, ref h, iDipX, iDipY, rot, color, false, false, ref iLength, 1000000); //间隔时间
TimeSpan subTract = DateTime.Now.Subtract(_StartDateTime);
double _ZhTime = subTract.TotalMilliseconds;
label1.Text = _ZhTime.ToString(); }
}
}
unsafe用这个可以在里面写非托管代码  这里要在项目里设置一下

												

c#引用c++dll和c++导出类出现的各种问题的更多相关文章

  1. 如何调用DLL中的导出类

    之前在网上一直查不到关于把类打包成dll文件的程序,今天自己写了个测试程序,供大家参考 一.生成类的dll文件 1.我是在vs2008上测试的,建立工程,在选择建立何种类型的工程的时候,勾上appli ...

  2. DLL导出类避免地狱问题的完美解决方案

    DLL动态链接库是程序复用的重要方式,DLL可以导出函数,使函数被多个程序复用,DLL中的函数实现可以被修改而无需重新编译和连接使用该DLL的应用程序.作为一名面向对象的程序员,希望DLL可以导出类, ...

  3. (转)C++类库开发之导出类设计原则

    上一篇博客详细陈述了类库开发的各个知识点(http://blog.csdn.net/z702143700/article/details/45989993),本文将进一步陈述,对于类库开发过程中导出类 ...

  4. DLL的概念、dll导出类(转贴)

    1. DLL的概念DLL(Dynamic Linkable Library),动态链接库,可以向程序提供一些函数.变量或类.这些可以直接拿来使用.静态链接库与动态链接库的区别:(1)静态链接库与动态链 ...

  5. DLL入门浅析(4)——从DLL中导出类

    转载自:http://www.cppblog.com/suiaiguo/archive/2009/07/20/90663.html 前面介绍了怎么从DLL中导出函数和变量,实际上导出类的方法也是大同小 ...

  6. DLL 导出类

    MyMathFun.h #pragma once // #ifdef DLLCLASS_API // #define DLLCLASS_API _declspec(dllimport) // #els ...

  7. C++ DLL导出类 知识大全

    在公司使用C++ 做开发,公司的大拿搭了一个C++的跨平台开发框架.在C++开发领域我还是个新手,有很多知识要学,比如Dll库的开发. 参考了很多这方面的资料,对DLL有一个基本全面的了解.有一个问题 ...

  8. MinGW dll导入导出类

    dll不仅可以导入导出函数,还可以导入导出类.这篇文章就来介绍如何将类导入dll中并导出. 首先我们建立一个名为dll.cpp的文件(又是这种破名字),里面写上: #include <iostr ...

  9. 实现MFC扩展DLL中导出类和对话框

    如果要编写模块化的软件,就要对对动态链接库(DLL)有一定的了解,本人这段时间在修改以前的软件时,决定把重复用的类和对话框做到DLL中,下面就从一个简单的例子讲起,如何实现MFC扩展DLL中导出类和对 ...

随机推荐

  1. MySQL实战45讲学习笔记:第六讲

    一.今日内容概要 今天我要跟你聊聊 MySQL 的锁.数据库锁设计的初衷是处理并发问题.作为多用户共享的资源,当出现并发访问的时候,数据库需要合理地控制资源的访问规则.而锁就是用来实现这些访问规则的重 ...

  2. [LeetCode] 75. Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  3. thinkphp5.1 - twig使用

    thinkphp5.1 - twig使用1.安装按照:https://github.com/yunwuxin/think-twigTwig Template For ThinkPHP5 安装 comp ...

  4. oracle--数据库扩容后出现ORA-27102

    一,问题描述 Connected to an idle instance. SQL> startup nomount ORA: obsolete or deprecated parameter( ...

  5. 手把手教你 通过 NuGet.Server 包 搭建nuget服务器,并使用桌面工具上传 nuget 包,免命令行

    新建web项目 工具:VS2013 版本:.Net Framework 4.6,低版本也行,不过要找到对应版本的Nuget.Server 装了NuGet客户端(百度如何安装) WebForm或MVC都 ...

  6. linux内核debug的一种方式:procfs

    #include <linux/module.h> #include <linux/compat.h> #include <linux/types.h> #incl ...

  7. Cookie,Session,Token and Oauth

    Cookie 服务器端生成,发送给客户端,保存用户信息.下一次请求同一网站时会把该cookie发送给服务器. 应用:登录表单自动填充,同样 随着交互式Web应用的兴起,像在线购物网站,需要登录的网站等 ...

  8. ant-design-pro引用css

    ant-design-pro中默认只能引用less文件,引用了css文件也是无效的.所以需要在配置文件config.js中找到  cssLoaderOptions,在 getLocalIdent中加入 ...

  9. CountdownLatch例子

    CountdownLatch 一个线程或者多个线程等待其他线程完成了再接着往下执行 public class CountDownLatchTest { ); private static Random ...

  10. RabbitMQ 在Windows环境下安装

    1. 下载RabbitMQ和Erlang RabbitMQ下载地址  https://www.rabbitmq.com/install-windows.html RabbitMQ是用Erlang编程语 ...