BCB創建dll和VS使用dll方法 [VSCallBCBDll]

BCB創建dll和VS使用dll方法 [VSCallBCBDll]

BCB創建dll和VS使用dll方法 [VSCallBCBDll]


GITHUB: https://github.com/jash-git/VSCallBCBDll


01.BCB_DLL [建立測試用DLL]

CPP

//---------------------------------------------------------------------------

#include <vcl.h>
#include <windows.h>
#pragma hdrstop
//---------------------------------------------------------------------------
//   Important note about DLL memory management when your DLL uses the
//   static version of the RunTime Library:
//
//   If your DLL exports any functions that pass String objects (or structs/
//   classes containing nested Strings) as parameter or function results,
//   you will need to add the library MEMMGR.LIB to both the DLL project and
//   any other projects that use the DLL.  You will also need to use MEMMGR.LIB
//   if any other projects which use the DLL will be performing new or delete
//   operations on any non-TObject-derived classes which are exported from the
//   DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
//   EXE's to use the BORLNDMM.DLL as their memory manager.  In these cases,
//   the file BORLNDMM.DLL should be deployed along with your DLL.
//
//   To avoid using BORLNDMM.DLL, pass string information using "char *" or
//   ShortString parameters.
//
//   If your DLL uses the dynamic version of the RTL, you do not need to
//   explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------

#pragma argsused
extern "C" __declspec(dllexport) int BCBType_Count(int m, int n);

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
{
        return 1;
}

int BCBType_Count(int m, int n)
{
   long sum = 0;

   for(m; m<=n; m++)
   {
      sum += m;
   }

   return sum;
}

//--------------------------------------------------------------------------- 


產生DEF的BAT

impdef.exe BCB_DLL.def BCB_DLL.dll


BCB_DLL_Old.def[原始產生]

LIBRARY     BCB_DLL.DLL

EXPORTS
    _BCBType_Count                 @1   ; _BCBType_Count
    ___CPPdebugHook                @2   ; ___CPPdebugHook


BCB_DLL.def[手動修改]

LIBRARY     BCB_DLL.DLL

EXPORTS
    BCBType_Count                 @1   ; _BCBType_Count
    ___CPPdebugHook                @2   ; ___CPPdebugHook



02.CreateVCLIB [建立VC使用的LIB]

建立VC專用LIB的BAT

LIB /DEF:BCB_DLL.def
pause



03.VCCallBCBDll [VC使用LIB呼叫BCB DLL]

CPP

// VCCallBCBDll.cpp : 定義主控台應用程式的進入點。
//

#include "stdafx.h"
#include <iostream>

using namespace std;
extern "C" __declspec(dllexport) int BCBType_Count(int m, int n);
void Pause()
{
	printf("Press Enter key to continue...");
	fgetc(stdin);
}
int main()
{
	cout << "Hello world!" << endl;
	cout << "1+2+3....+100=" << BCBType_Count(0, 100) << endl;
	Pause();
	return 0;
}



04.CSCallBCBDll [C#呼叫BCB DLL]

CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Runtime.InteropServices;//USE DLL

namespace CSCallBCBDll
{
    class Program
    {
        [DllImport("./BCB_DLL.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, EntryPoint = "_BCBType_Count")]
        public static extern int BCBType_Count(int m, int n);

        static void Pause()
        {
            Console.Write("Press any key to continue...");
            Console.ReadKey(true);
        }

        static void Main(string[] args)
        {
            Console.WriteLine("1+2+3+...100={0}", BCBType_Count(0, 100));
            Pause();
        }
    }
}



05.CBCallBCBDll [純C++ 利用WINDOWS API『LoadLibrary』直接使用CB DLL函數]

#include <iostream>
#include <stdio.h>
using namespace std;

//---
//不使用LIB動態載入DLL函數
//http://www.infernodevelopment.com/simple-c-dll-loading-message-box
#include<windows.h>
typedef  int (*FuncPtr)(int m,int n);
//---不使用LIB動態載入DLL函數

void Pause()
{
    printf("Press Enter key to continue...");
    fgetc(stdin);
}
int main()
{
    FuncPtr myDllFunc;
    BOOL linkSuccessFlag = FALSE, fFreeResult;
    HINSTANCE hinstLib = LoadLibrary("BCB_DLL.dll");

    if (hinstLib != NULL)
    {
        myDllFunc = (FuncPtr) GetProcAddress(hinstLib, "_BCBType_Count");

        if (myDllFunc != NULL)
        {
            cout << "1+2+3+...+100=" << myDllFunc(1,100) <<endl;
        }
        fFreeResult = FreeLibrary(hinstLib);
    }
    else
    {
        cout << "Hello world!" << endl;
    }


    Pause();
    return 0;
}

One thought on “BCB創建dll和VS使用dll方法 [VSCallBCBDll]

  1. 最近可能又要亂搞,所以先將亂搞技術準備好
    原來BCB 開發的DLL+LIB 不能直接給VC 使用
    好險有GOOGLE 幫忙 終於找到解決方法 有需要請自取
    PS. 原來 沒有LIB檔案的DLL 可以利用VC內建工具 建立出來

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *