Výsledky 1 až 2 z 2

Téma: MS VS .Net a Class v Dll

  1. #1

    Standardní MS VS .Net a Class v Dll

    Ako exportovat triedu z Dll? A ako ju potom pouzit v inom programe?

    Som skusal uz vsetko mozne, ale pre moju neschopnost to stale nejde...

    V sucastnosti mam nieco taketo:
    Projekt TestClassDll
    subor stdafx.h, na konci pridane riadky:
    Kód:
    #define _TESTCLASS_DLLAPI_
    #define _TESTCLASS_NOAUTOLIB_
    subor TestClassDll.h, pridaval som prve dva odstavce (ale davam ho sem cely)
    Kód:
    // TestClassDll.h : main header file for the TestClassDll DLL
    //
    
    #ifdef _TESTCLASS_DLLAPI_
    	#define TESTCLASS_DLLAPI __declspec( dllexport )
    #else
    	#define TESTCLASS_DLLAPI __declspec( dllexport )
    #endif
    
    #ifndef _TESTCLASS_NOAUTOLIB_
    #ifdef _DEBUG
    #pragma comment(lib, "TestClassDlld.lib")
    #else
    #pragma comment(lib, "TestClassDll.lib")
    #endif
    #endif
    
    #pragma once
    
    #ifndef __AFXWIN_H__
    	#error include 'stdafx.h' before including this file for PCH
    #endif
    
    #include "resource.h"		// main symbols
    
    
    // CTestClassDllApp
    // See TestClassDll.cpp for the implementation of this class
    //
    
    class CTestClassDllApp : public CWinApp
    {
    public:
    	CTestClassDllApp();
    
    // Overrides
    public:
    	virtual BOOL InitInstance();
    
    	DECLARE_MESSAGE_MAP()
    };
    subor TestClass.h
    Kód:
    #pragma once
    
    #ifdef _TESTCLASS_DLLAPI_
    	#define TESTCLASS_DLLAPI __declspec( dllexport )
    #else
    	#define TESTCLASS_DLLAPI __declspec( dllexport )
    #endif
    //#define MY_EXPORT __declspec( dllexport )
    
    TESTCLASS_DLLAPI int MyFunc(int n);
    
    int MyFunc(int n)
    {
    	return 6;
    }
    
    class TESTCLASS_DLLAPI TestClass
    {
    public:
    	TestClass(void);
    	~TestClass(void);
    	CString GetName(void);
    	CString name;
    	void SetName(CString n);
    };
    subor TestClass.cpp
    Kód:
    #include "StdAfx.h"
    #include "testclass.h"
    
    TestClass::TestClass(void)
    : name(_T(""))
    {
    	name = "TestClass";
    }
    
    TestClass::~TestClass(void)
    {
    }
    
    CString TestClass::GetName(void)
    {
    	return CString(name);
    }
    
    void TestClass::SetName(CString n)
    {
    	name = n;
    }
    --------------

    Projekt TestUsingClassDll
    subor stdafx.h bez zmeny
    subor TestUsingClassDll.cpp
    Kód:
    // TestUsingClassDll.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    #define MY_IMPORT __declspec( dllimport )
    
    MY_IMPORT int MyFunc(int n);
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	MY_IMPORT class TestClass {} *TstClss;
    //	TstClss = new TestClass(); // komentar 1
    //	TstClss.SetName("nazov"); // komentar 2
    	printf("%d", MyFunc(17));
    	getchar();
    	return MyFunc(17);
    }
    -------------

    Pokial zrusim komentovanie komentara 1 (posledny zdrojovy subor) vznikne mi chyba pri linkovani:
    Kód:
    TestUsingClassDll error LNK2001: unresolved external symbol "__declspec(dllimport) class TestClass * TstClss" (__imp_?TstClss@@3PAVTestClass@@A)
    TestUsingClassDll fatal error LNK1120: 1 unresolved externals
    Pokial zrusim * pred TstClss (takze to uz nebude smernik), a zrusim komentovanie komentara 2, vznikne nasledujuca chyba:
    Kód:
    c:\Documents and Settings\matej.W2003SVR04\My Documents\Visual Studio Projects\TestUsingClassDll\TestUsingClassDll.cpp(15): error C2039: 'SetName' : is not a member of '(int,_TCHAR *[])main::TestClass'
    Najvacsia sranda je, ze pokial obidva komentare necham, skompiluje sa to i zlinkuje, a bez problemov to vypise "6", na co pouzije funkciu z tej Dll.

    Ak viete co s tym, prosim poradte. Trapim sa s tym uz vyse tyzden a stale neviem z Dll exportovat triedu a pouzit tu triedu v programe ktory by pouzival tu Dll.
    zostava1 - zostava2 - zostava3 - zostava4
    TD2: You lunatic! There are women and children out there you could\'ve killed.

  2. #2

    Standardní Re: MS VS .Net a Class v Dll

    Citace Původně odeslal Lunatic
    Kód:
    #ifdef _TESTCLASS_DLLAPI_
    	#define TESTCLASS_DLLAPI __declspec( dllexport )
    #else
    	#define TESTCLASS_DLLAPI __declspec( dllexport )
    #endif
    Som si predchvilkou vsimol chybu. Ale ani jej oprava nic nevyriesila.
    Kód:
    #ifdef _TESTCLASS_DLLAPI_
    	#define TESTCLASS_DLLAPI __declspec( dllexport )
    #else
    	#define TESTCLASS_DLLAPI __declspec( dllimport )
    #endif
    zostava1 - zostava2 - zostava3 - zostava4
    TD2: You lunatic! There are women and children out there you could\'ve killed.

Informace o tématu

Users Browsing this Thread

Toto téma si právě prohlíží 1 uživatelů. (0 registrovaných a 1 anonymních)

Pravidla přispívání

  • Nemůžete zakládat nová témata
  • Nemůžete zasílat odpovědi
  • Nemůžete přikládat přílohy
  • Nemůžete upravovat své příspěvky
  •