最近对一些第三方类库进行c++托管以便c#调用 由于以前没弄过,出现各类各样的问题c++
fatal error LNK1104: 没法打开文件“xxx.lib”或者xxx.dll 等等等c#
总结:spa
1.字符集:设置同样3d
2.平台:设置同样,好比32位 就都设置32位,这里千万要注意:设置平台的时候要一个个看清楚才行 否则在上面设置了解决方案,下面项目有些没反应(新建的)指针
3.引用进来的dll还有头文件,若是头文件中引用了其余的文件,记得统统给它导进来了code
4.这样还不必定行,还会出现各类未识别,未能解析提示,看你的要引用的头文件是否是须要引用其余的.lib文件 ,都引用进来试试orm
5.好了,没报错 可是你建立的c#引用c++dll报错了,未能加载dll,那你要看看托管的工程dll和托管要包装的dll有没有复制到c#项目运行目录下了blog
6.若是仍是不行 c#的项目平台是否是都是同样的ip
7.仍是不行,用管理员运行你的vs 或者能够先用管理员运行exe看行不行。string
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 2; } 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(1); int h = 0; //CPdf.GetHeight();//获得pdf默认高度 int w = 0; //CPdf.GetWidth(); //CPdf.SetAlphaBits(4); //int iLength = 0;//总字节数w*h*(ch+alph) float iDipX = 600;//若是指定了w,不起做用 float iDipY = 600;//若是指定了h,不起做用 int rot = 0;//-90,90,180 int color = 1;//0:rgba 1:ga byte[] date; unsafe { int iLength = 0; byte* intP = mup.TGetBitmap(w, h, iDipX, iDipY, rot, color, false, false, &iLength, 1000000); date = new byte[iLength]; //用下面的赋值多200多毫秒 //for (int i = 0; i < iLength; i++) //{ // date[i] = *intP; // intP++; //} //用这个用时较少 Marshal.Copy((IntPtr)intP, date, 0, 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用这个能够在里面写非托管代码 这里要在项目里设置一下