最近對一些第三方類庫進行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用這個能夠在裏面寫非託管代碼 這裏要在項目裏設置一下