最近对一些第三方类库进行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用这个可以在里面写非托管代码  这里要在项目里设置一下

												

最新文章

  1. php+websocket搭建简易聊天室实践
  2. js_事件委托
  3. OpenJudge1700:八皇后问题 //不属于基本法的基本玩意
  4. 【poj1041】 John&#39;s trip
  5. 【JAVA 其它流对象】
  6. shiro 简单的身份验证 案例
  7. USACO Section 3.3 商店购物 Shopping Offers
  8. bzoj1487
  9. Python 基础-python-列表-元组-字典-集合
  10. Oracle遇到的一些问题
  11. jquery 弹出登陆框,简单易懂!修改密码效果代码
  12. uva 10817 Headmaster&amp;#39;s Headache 出发dp 位计算
  13. 笔记:Hibernate SQL 查询
  14. P1352 没有上司的舞会
  15. asp.net 客户端请求到响应的整个过程
  16. Spring 学习——Spring AOP——AOP配置篇Advice(有参数传递)
  17. ucos之互斥信号量及优先级反转
  18. IDEA项目搭建十四——Web站点Controller基类及布局页静态资源设计
  19. mysql定时任务,每天的零点执行一个存储过程
  20. HDOJ2013_蟠桃记

热门文章

  1. 【PE512】Sums of totients of powers(欧拉函数)
  2. 【递归】执行过程探究(c)
  3. (转)yum只下载不安装软件包
  4. python使用rdflib创建rdf,在jena fuseki上执行SPARQL查询
  5. win10下MYSQL的下载、安装以及配置超详解教程(转)
  6. SQL -------- TOP 查询前几行
  7. useEffect传入第二个参数陷入死循环
  8. 用友U9 刷新当前页面代码
  9. 万张PubFig人脸数据实现基于python+OpenCV的人脸特征定位程序(1)
  10. 【开发笔记】- grails框架中在使用domain的save方法保存时保存不成功