顯示具有 dll 標籤的文章。 顯示所有文章
顯示具有 dll 標籤的文章。 顯示所有文章

2016年4月22日 星期五

[C++] Visual Studio 遇到ATL Com Crash的問題(解決方案)

在做IMAPI的東西的時候,需要用到CoCOM的東西,

然後他又需要使用ATL....總之很麻煩0rz


好了,重點就是在trace bug的時候跑到atlcom.h這個檔案裡面的

class CComObjectCached : public Base
{
public:
typedef Base _BaseClass;
CComObjectCached(void* = NULL){}
// Set refcount to -(LONG_MAX/2) to protect destruction and
// also catch mismatched Release in debug builds
// This will be made virtual again for Beta 2
/*virtual*/ ~CComObjectCached()
{
m_dwRef = -(LONG_MAX/2);
FinalRelease();
#ifdef _ATL_DEBUG_INTERFACES
_AtlDebugInterfacesModule.DeleteNonAddRefThunk(_GetRawUnknown());
#endif
}
//If InternalAddRef or InternalRelease is undefined then your class
//doesn't derive from CComObjectRoot
STDMETHOD_(ULONG, AddRef)() throw()
{
ULONG l = InternalAddRef();
if (l == 2)
_pAtlModule->Lock();
return l;
}

沒錯,就是_pAtlModule->Lock();這邊,然後你發現他記憶體位址居然是0x000000

然後就crash了!


不要相信甚麼add ATL support to MFC 之類的鬼話(加了以後我不知道怎麼刪掉,只能重寫)

解決方法:

在你的主程式(.cpp)裡面,

#include .......

//在這邊新增
CComModule _Module;
extern __declspec(selectany) CAtlModule* _pAtlModule=&_Module;

//新增完成

主程式:建構子....

主程式.....

這樣就可以解決了

END

2015年9月22日 星期二

[Qt] 初次使用Qt Creator

Qt是一個跨平台語言,也就是說可以放在iOS,Android,Linux,Mac,Windows....等等都共通的語言

Qt Creator 我使用的版本是: 4.6.2


語法目前觀察是滿像C++,不過我對C++沒有很熟就是了0rz

整個project會包含.ui / .h / .cpp  / .pro


.h就是標頭檔
.ui就是ui介面
.cpp就是主程式
.pro不太清楚但應該是類似qmakefile 用的

如果你要輸出的話,會必須要把下圖中這些檔案放進debug資料夾,否則你的exe檔無法執行


(error debug)如果遇到Id return 1 exit status的話,那就代表你已經在執行程式,但又在run一次,把目前程式關掉,才能run!

END

2014年4月22日 星期二

[C#] 如何打包exe檔 與 dll 檔?

相信很多人google關鍵字一定是會出現ILMerge這個工具,但今天要講得不是這個工具。

今天要介紹的是一個叫"Xenocode Postbuild 2010 for .Net" *(年份可能會更新,目前最新版本為

2010(好像也有看到2013,但不確定)



首先,進入Application ->Add(加入你要的dll + exe)


一次只能放一個exe檔,但可以多個dll檔


進入virtrualize->Enable(打勾)->Runtimes->Enabled .NET runtime engine: (選擇要的版本)

(底下有SQL,但我沒用到所以就沒選擇)



最後在Output分頁這邊,有一個Single application executable(當然你要選multiple application也是可以,

但功能不同)選曲要輸出的exe檔案後->按下Build Application->完成!

然後你的程式就可以在 "非.net環境下"(沒有安裝.net framework)執行!

低調載點: .

End

2014年2月10日 星期一

[C#/Serialize] 在不同檔案間傳遞結構(structure)利用Serialize和Deserialize

首先必須先創建一個"類別庫"(新增->類別庫 in VS2012)

一定要有關鍵字[Serializable]否則無法,然後建立一個Struct

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.ComponentModel;

namespace PassingStruct
{
    public class Class1
    {
    }

    [Serializable]
    public struct MemberList
    {
        private BindingList<Member> _MemberCollection;
        public BindingList<Member> MemberCollection
        {
            get
            {
                if (this._MemberCollection == null)
                    this._MemberCollection = new BindingList<Member>();
                return this._MemberCollection;
            }
        }
    }

    [Serializable]
    public struct Member
    {
        public int ID { get; set; }
        public int age { get; set; }
        public string name { get; set; }
        public int number { get; set; }
        public string sex { get; set; }
    }
}

編譯時會產生錯誤訊息,但其實還是成功(在Debug資料夾內)會有一個dll檔產生。

OK,到這邊,需要將dll檔加入參考到要序列化的程式碼。

以下為序列化程式碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using PassingStruct;

namespace Binary_filestreame
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
         
        }

        private void button1_Click(object sender, EventArgs e)
        {

            PassingStruct.MemberList list = CreateClass();
            SerializeToBinary("list.dat", list);
        }
        public PassingStruct.MemberList CreateClass()
        {
            PassingStruct.MemberList list = new PassingStruct.MemberList();
            list.MemberCollection.Add(new PassingStruct.Member() { ID = 1, age = 18, name = "paul walker", number = 999, sex = "male" });
            list.MemberCollection.Add(new PassingStruct.Member() { ID = 2, age = 20, name = "jonathan brewster", number = 123, sex = "female" });
            return list;
        }
        public static void SerializeToBinary(string FileName, object Object)
        {
            Stream stream = null;
            IFormatter/*用於格式化已經序列化的東西*/ format = new BinaryFormatter();//以二進位格式序列化和還原序列化物件
            try
            {
                stream = new FileStream(FileName,FileMode.Create,FileAccess.Write,FileShare.Read);
                format.Serialize(stream, Object);
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                stream.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            PassingStruct.MemberList list = DeserializeFromBinary<PassingStruct.MemberList>("list.dat");
        }

        public static T DeserializeFromBinary<T>(string FileName)
        {
            IFormatter formatter = new BinaryFormatter();
            Stream stream = null;
            try
            {
                stream = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                object obj = formatter.Deserialize(stream);
                if (obj == null)
                    return default(T);
                else
                    return (T)obj;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                stream.Close();
            }
        }
    }
}

注意到在using這邊
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using PassingStruct;//這個就是dll檔,除了在Visual Studio專案裡面選擇 專案->加入參考->瀏覽->dll檔,還要加入這一行

然後,序列化基本上就完成了。接著就是反序列化:(以下為反序列化程式碼)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using PassingStruct;

namespace serialize_transfer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //File.wr
        }

        public static T DeserializeFromBinary<T>(string FilePath)
        {
            IFormatter formatter = new BinaryFormatter();
            Stream stream = null;
            try
            {
                stream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                object obj = formatter.Deserialize(stream);//問題出在這邊
                if (obj == null)
                    return default(T);
                else
                    return (T)obj;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                stream.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PassingStruct.MemberList list = DeserializeFromBinary<PassingStruct.MemberList>(@"C:\Users\daniel\Dropbox\Visual Studio 2012\Projects\Binary_filestreame\Binary_filestreame\bin\Debug\list.dat");
        }
    }
}

同樣也需要加入參考以及+using的程式碼 記得反序列化路徑要弄對就好了。

應用的部分,可使用在DataGridView上面

BindingSource Source = new BindingSource();
public void Form1_Load(object sender, EventArgs e)
{
    this.Source.DataSource = CreateClass().MemberCollection;

    this.dataGridView1.DataSource = this.Source;
    this.bindingNavigator1.BindingSource = this.Source;
    this.textBox1.DataBindings.Add("Text", this.Source, "ID");
    this.textBox2.DataBindings.Add("Text", this.Source, "name");
    this.textBox3.DataBindings.Add("Text", this.Source, "age");
    this.textBox4.DataBindings.Add("Text", this.Source, "phone");
    this.textBox5.DataBindings.Add("Text", this.Source, "sex");
}

End

參考資料: 余小章.net教學