<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[gelöst] brauche Hilfe mit template methoden]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich bin schon seit 24h am ausprobieren und komme langsam nicht mehr weiter.</p>
<p>Worum es geht:<br />
Ich möchte eine allgemeine Lösung finden wie man am besten DLL dynamisch einbindet. Naürlich habe ich das schon &quot;konventionel&quot; geschafft und möchte jetzt so eine Art Vorlage für zukünftige Projekte schreiben.<br />
Dazu habe ich 3 Projecte im ProjectOrdner erstellt:</p>
<p>application.exe<br />
interface.lib<br />
calculator.dll</p>
<p><strong>Bevor jetzt einer denkt es gehe um WinAPI und aufhört zu lesen,da falsches Forum möchte ich schnell sagen das dem nicht so ist und um Geduld bitten. Vieleicht kann man mir besser helfen wenn man weiß worum es geht.</strong></p>
<p>Ich habe vereinfacht folgenden code:</p>
<p><strong>application</strong><br />
<em>main.cpp</em></p>
<pre><code class="language-cpp">#include &quot;INTERFACE.h&quot;
#include &quot;DLL_MANAGER.h&quot;
#include &lt;iostream&gt;
using namespace std;

int main()
{
	iiDLL_MANAGER iDevice;
	iiCALCULATOR_INTERFACE *calculatorDevice = 0;

	iDevice.LoadEngineDevice(L&quot;multiply.dll&quot;);
	iDevice.GetDeviceHandle(calculatorDevice);
	calculatorDevice-&gt;Multiply(4, 4);
	calculatorDevice-&gt;Devide(10, 2);
}
</code></pre>
<p><strong>interface</strong><br />
<em>INTERFACE.h</em> //abstrakte Klasse</p>
<pre><code class="language-cpp">#pragma once
class iiCALCULATOR_INTERFACE
{
public:
	virtual int Multiply(int, int) = 0;
	virtual int Devide(int, int) = 0;
	virtual void Release() = 0;
};

extern &quot;C&quot; __declspec(dllexport) 
	iiCALCULATOR_INTERFACE *GetInterfaceImplementation();
</code></pre>
<p>immernoch<br />
<strong>interface</strong><br />
<em>DLL_MANAGER.h</em></p>
<pre><code class="language-cpp">#pragma once
#include &lt;windows.h&gt;

class iiDLL_MANAGER
{
public:
	iiDLL_MANAGER(void);
	~iiDLL_MANAGER(void);

	void	LoadEngineDevice(LPCWSTR libraryName);

        template&lt;typename T&gt;
	void	GetDeviceHandle(T deviceType);              //darum geht es
	void	ReleaseEngineDevice();
private:
	HMODULE hDll;
};
</code></pre>
<p><strong>interface</strong><br />
<em>DLL_MANAGER.cpp</em></p>
<pre><code class="language-cpp">#include &quot;iiDLL_MANAGER.h&quot;
#include &lt;iostream&gt;
using namespace std;

iiDLL_MANAGER::iiDLL_MANAGER(void)
{
}

iiDLL_MANAGER::~iiDLL_MANAGER(void)
{
}

void iiDLL_MANAGER::LoadEngineDevice(LPCWSTR libraryName)
{	       hDll=0;																
hDll=LoadLibrary(libraryName);										
cout&lt;&lt;&quot;Loading library was successful.\n&quot;	
}

template&lt;typename T&gt;
void iiDLL_MANAGER::GetDeviceHandle(T deviceType)
{
   	T deviceType = (T)GetProcAddress(hDll, &quot;GetInterfaceImplementation&quot;);
}

void         iiDLL_MANAGER::ReleaseEngineDevice()
{
    //TODO
}
</code></pre>
<p><strong>calculator</strong><br />
<em>SLOW_CALCULATOR.h</em></p>
<pre><code class="language-cpp">#pragma once
#include &quot;iiCALCULATOR_INTERFACE.h&quot;
#include &lt;iostream&gt;

class iiSLOWCALCULATOR : public iiCALCULATOR_INTERFACE  
{
public:
	iiSLOWCALCULATOR(void);
	~iiSLOWCALCULATOR(void);

	int Multiply(int factor1, int factor2);
	int Devide(int devident, int devisor);
	void Release();
};
</code></pre>
<p><strong>calculator</strong><br />
<em>SLOW_CALCULATOR.cpp</em></p>
<pre><code class="language-cpp">#include &quot;iiSLOWCALCULATOR.h&quot;

iiSLOWCALCULATOR::iiSLOWCALCULATOR(void)
{
}

iiSLOWCALCULATOR::~iiSLOWCALCULATOR(void)
{
}

int iiSLOWCALCULATOR::Multiply(int factor1, int factor2)
{
	std::cout&lt;&lt;factor1*factor2 &lt;&lt;std::endl;
	return (factor1 * factor2);										
}
int iiSLOWCALCULATOR::Devide(int devident, int devisor)
{
	std::cout&lt;&lt;devident/devisor &lt;&lt;std::endl;
	return (devident/devisor);
}
void iiSLOWCALCULATOR::Release()
{
	delete this;
}

 iiCALCULATOR_INTERFACE *GetInterfaceImplementation()         //!!!
{
	return new iiSLOWCALCULATOR;
}
</code></pre>
<p>Jetzt noch schnell die Fehlermeldung:</p>
<blockquote>
<p>3&gt;ClCompile:<br />
3&gt; main.cpp<br />
3&gt;main.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;&quot;public: void __thiscall iiDLL_MANAGER::GetDeviceHandle&lt;int&gt;(int)&quot; (??$GetDeviceHandle@H@iiDLL_MANAGER@@QAEXH@Z)&quot; in Funktion &quot;_main&quot;.<br />
3&gt;C:\Dokumente und Einstellungen\Ghost\Eigene Dateien\Visual Studio 2008\Projects\dynamik DLL link\Debug\application.exe : fatal error LNK1120: 1 nicht aufgelöste externe Verweise.<br />
3&gt;<br />
3&gt;Fehler beim Erstellen<br />
3&gt;<br />
3&gt;Verstrichene Zeit 00:00:02.43<br />
========== Alles neu erstellen: 2 erfolgreich, Fehler bei 1, 0 übersprungen ==========</p>
</blockquote>
<p>Mein Problem ist allem Anschein nach die template Funktion da es keinerlei Probleme gibt wenn ich diese Funktion fest &quot;verdrahte&quot;. Mein gedanke war aber das ich die Klasse DLL_MANAGER für alle INTERFACE typen benutzen kann.<br />
Ich übergebe also den Dateinamen und einen Zeiger des Zieltyps - die dll wird geladen und der Zeiger mit der Adresse des erstellten Objects initialisiert.</p>
<p>Ich möchte nochmals sagen dass es mir nicht um das laden der dll geht sondern wie ich die template funktion richtig nutze.</p>
<p>Vielen Dank</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/276227/gelöst-brauche-hilfe-mit-template-methoden</link><generator>RSS for Node</generator><lastBuildDate>Wed, 26 Aug 2026 11:34:41 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/276227.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 29 Oct 2010 10:07:45 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [gelöst] brauche Hilfe mit template methoden on Sun, 31 Oct 2010 12:34:33 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich bin schon seit 24h am ausprobieren und komme langsam nicht mehr weiter.</p>
<p>Worum es geht:<br />
Ich möchte eine allgemeine Lösung finden wie man am besten DLL dynamisch einbindet. Naürlich habe ich das schon &quot;konventionel&quot; geschafft und möchte jetzt so eine Art Vorlage für zukünftige Projekte schreiben.<br />
Dazu habe ich 3 Projecte im ProjectOrdner erstellt:</p>
<p>application.exe<br />
interface.lib<br />
calculator.dll</p>
<p><strong>Bevor jetzt einer denkt es gehe um WinAPI und aufhört zu lesen,da falsches Forum möchte ich schnell sagen das dem nicht so ist und um Geduld bitten. Vieleicht kann man mir besser helfen wenn man weiß worum es geht.</strong></p>
<p>Ich habe vereinfacht folgenden code:</p>
<p><strong>application</strong><br />
<em>main.cpp</em></p>
<pre><code class="language-cpp">#include &quot;INTERFACE.h&quot;
#include &quot;DLL_MANAGER.h&quot;
#include &lt;iostream&gt;
using namespace std;

int main()
{
	iiDLL_MANAGER iDevice;
	iiCALCULATOR_INTERFACE *calculatorDevice = 0;

	iDevice.LoadEngineDevice(L&quot;multiply.dll&quot;);
	iDevice.GetDeviceHandle(calculatorDevice);
	calculatorDevice-&gt;Multiply(4, 4);
	calculatorDevice-&gt;Devide(10, 2);
}
</code></pre>
<p><strong>interface</strong><br />
<em>INTERFACE.h</em> //abstrakte Klasse</p>
<pre><code class="language-cpp">#pragma once
class iiCALCULATOR_INTERFACE
{
public:
	virtual int Multiply(int, int) = 0;
	virtual int Devide(int, int) = 0;
	virtual void Release() = 0;
};

extern &quot;C&quot; __declspec(dllexport) 
	iiCALCULATOR_INTERFACE *GetInterfaceImplementation();
</code></pre>
<p>immernoch<br />
<strong>interface</strong><br />
<em>DLL_MANAGER.h</em></p>
<pre><code class="language-cpp">#pragma once
#include &lt;windows.h&gt;

class iiDLL_MANAGER
{
public:
	iiDLL_MANAGER(void);
	~iiDLL_MANAGER(void);

	void	LoadEngineDevice(LPCWSTR libraryName);

        template&lt;typename T&gt;
	void	GetDeviceHandle(T deviceType);              //darum geht es
	void	ReleaseEngineDevice();
private:
	HMODULE hDll;
};
</code></pre>
<p><strong>interface</strong><br />
<em>DLL_MANAGER.cpp</em></p>
<pre><code class="language-cpp">#include &quot;iiDLL_MANAGER.h&quot;
#include &lt;iostream&gt;
using namespace std;

iiDLL_MANAGER::iiDLL_MANAGER(void)
{
}

iiDLL_MANAGER::~iiDLL_MANAGER(void)
{
}

void iiDLL_MANAGER::LoadEngineDevice(LPCWSTR libraryName)
{	       hDll=0;																
hDll=LoadLibrary(libraryName);										
cout&lt;&lt;&quot;Loading library was successful.\n&quot;	
}

template&lt;typename T&gt;
void iiDLL_MANAGER::GetDeviceHandle(T deviceType)
{
   	T deviceType = (T)GetProcAddress(hDll, &quot;GetInterfaceImplementation&quot;);
}

void         iiDLL_MANAGER::ReleaseEngineDevice()
{
    //TODO
}
</code></pre>
<p><strong>calculator</strong><br />
<em>SLOW_CALCULATOR.h</em></p>
<pre><code class="language-cpp">#pragma once
#include &quot;iiCALCULATOR_INTERFACE.h&quot;
#include &lt;iostream&gt;

class iiSLOWCALCULATOR : public iiCALCULATOR_INTERFACE  
{
public:
	iiSLOWCALCULATOR(void);
	~iiSLOWCALCULATOR(void);

	int Multiply(int factor1, int factor2);
	int Devide(int devident, int devisor);
	void Release();
};
</code></pre>
<p><strong>calculator</strong><br />
<em>SLOW_CALCULATOR.cpp</em></p>
<pre><code class="language-cpp">#include &quot;iiSLOWCALCULATOR.h&quot;

iiSLOWCALCULATOR::iiSLOWCALCULATOR(void)
{
}

iiSLOWCALCULATOR::~iiSLOWCALCULATOR(void)
{
}

int iiSLOWCALCULATOR::Multiply(int factor1, int factor2)
{
	std::cout&lt;&lt;factor1*factor2 &lt;&lt;std::endl;
	return (factor1 * factor2);										
}
int iiSLOWCALCULATOR::Devide(int devident, int devisor)
{
	std::cout&lt;&lt;devident/devisor &lt;&lt;std::endl;
	return (devident/devisor);
}
void iiSLOWCALCULATOR::Release()
{
	delete this;
}

 iiCALCULATOR_INTERFACE *GetInterfaceImplementation()         //!!!
{
	return new iiSLOWCALCULATOR;
}
</code></pre>
<p>Jetzt noch schnell die Fehlermeldung:</p>
<blockquote>
<p>3&gt;ClCompile:<br />
3&gt; main.cpp<br />
3&gt;main.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;&quot;public: void __thiscall iiDLL_MANAGER::GetDeviceHandle&lt;int&gt;(int)&quot; (??$GetDeviceHandle@H@iiDLL_MANAGER@@QAEXH@Z)&quot; in Funktion &quot;_main&quot;.<br />
3&gt;C:\Dokumente und Einstellungen\Ghost\Eigene Dateien\Visual Studio 2008\Projects\dynamik DLL link\Debug\application.exe : fatal error LNK1120: 1 nicht aufgelöste externe Verweise.<br />
3&gt;<br />
3&gt;Fehler beim Erstellen<br />
3&gt;<br />
3&gt;Verstrichene Zeit 00:00:02.43<br />
========== Alles neu erstellen: 2 erfolgreich, Fehler bei 1, 0 übersprungen ==========</p>
</blockquote>
<p>Mein Problem ist allem Anschein nach die template Funktion da es keinerlei Probleme gibt wenn ich diese Funktion fest &quot;verdrahte&quot;. Mein gedanke war aber das ich die Klasse DLL_MANAGER für alle INTERFACE typen benutzen kann.<br />
Ich übergebe also den Dateinamen und einen Zeiger des Zieltyps - die dll wird geladen und der Zeiger mit der Adresse des erstellten Objects initialisiert.</p>
<p>Ich möchte nochmals sagen dass es mir nicht um das laden der dll geht sondern wie ich die template funktion richtig nutze.</p>
<p>Vielen Dank</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1972280</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1972280</guid><dc:creator><![CDATA[socco]]></dc:creator><pubDate>Sun, 31 Oct 2010 12:34:33 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] brauche Hilfe mit template methoden on Fri, 29 Oct 2010 10:38:58 GMT]]></title><description><![CDATA[<p>Wozu Templates? Du hast doch deine Basisklasse, also kannst du doch einfach in jeder deiner Libs eine Methode &quot;GetDeviceImplementation&quot; definieren, die einen Pointer auf ein iiCALCULATOR_INTERFACE zurückgibt. Die Methode erstellt dann ein neues Objekt des jeweiligen Typen und du kannst ganz normal auf die Methoden zugreifen. Beispiel:</p>
<p><strong>base.h</strong></p>
<pre><code class="language-cpp">class base
{
    public:
        virtual void doSomething() = 0;
};

extern &quot;C&quot; base *getInstance();
</code></pre>
<p><strong>derived.h</strong></p>
<pre><code class="language-cpp">class derived : public base
{
    public:
        void doSomething();
};
</code></pre>
<p><strong>derived.cpp</strong> (wird als statische/dynamische Lib kompiliert)</p>
<pre><code class="language-cpp">void derived::doSomething()
{
    std::cout &lt;&lt; &quot;derived&quot;;
}

base *getInstance()
{
    return new derived;
}
</code></pre>
<p>Bei einer statischen Lib könntest du dann in einem Projekt folgendermaßen drauf zugreifen:</p>
<pre><code class="language-cpp">int main()
{
    base *d = getInstance();

    d-&gt;doSomething(); // gibt &quot;derived&quot; aus

    delete d;

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1972289</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1972289</guid><dc:creator><![CDATA[Schablonenbändiger]]></dc:creator><pubDate>Fri, 29 Oct 2010 10:38:58 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] brauche Hilfe mit template methoden on Fri, 29 Oct 2010 11:13:47 GMT]]></title><description><![CDATA[<p>aber dann müsste CALCULATOR_INTERFACE alle interface funktionen enthalter auch die von z.B.: AUDIO_INTERFACE.<br />
Ich möchte zu jeder DLL eine interface KLasse schleiben von der dann geerbt wird und die dann überschrieben wird.<br />
Ich möchte mit dem DLL_MANAGER auch AUDIO_INTERFACE usw. laden können.</p>
<p>Kannst du denn erkennen warum ein linkfehler entsteht?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1972291</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1972291</guid><dc:creator><![CDATA[socco]]></dc:creator><pubDate>Fri, 29 Oct 2010 11:13:47 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] brauche Hilfe mit template methoden on Fri, 29 Oct 2010 12:20:27 GMT]]></title><description><![CDATA[<p>Template-Definitionen müssen im Header platziert werden, da sonst der Linker diese Funktionen nicht kennen kann (und daher auch die Fehlermeldung kommt).</p>
<p>Richtig also:</p>
<pre><code class="language-cpp">// in DLL_MANAGER.h

template&lt;typename T&gt;
void iiDLL_MANAGER::GetDeviceHandle(T deviceType)
{
  T deviceType = (T)GetProcAddress(hDll, &quot;GetInterfaceImplementation&quot;);
}
</code></pre>
<p>Entweder direkt in der Klasse definieren (ohne 'iiDLL_MANAGER::') oder aber unterhalb der Klasse anordnen bzw. sogar in eigene Datei 'dll_manager.inl' auslagern und unten in der Headerdatei ein</p>
<pre><code class="language-cpp">#include &quot;dll_manager.inl&quot;
</code></pre>
<p>s.a. <a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-39467.html" rel="nofollow">FAQ: Template in Header und Source aufteilen</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1972320</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1972320</guid><dc:creator><![CDATA[Th69]]></dc:creator><pubDate>Fri, 29 Oct 2010 12:20:27 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] brauche Hilfe mit template methoden on Fri, 29 Oct 2010 17:31:52 GMT]]></title><description><![CDATA[<p>Ja alles klar das war der Fehler.<br />
Danke.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1972454</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1972454</guid><dc:creator><![CDATA[socco]]></dc:creator><pubDate>Fri, 29 Oct 2010 17:31:52 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] brauche Hilfe mit template methoden on Fri, 29 Oct 2010 17:40:07 GMT]]></title><description><![CDATA[<p>Devide -&gt; Divide</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1972457</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1972457</guid><dc:creator><![CDATA[padreigh]]></dc:creator><pubDate>Fri, 29 Oct 2010 17:40:07 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] brauche Hilfe mit template methoden on Sat, 30 Oct 2010 17:09:08 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/23744">@padreigh</a> danke, sehr aufmerksam.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1972776</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1972776</guid><dc:creator><![CDATA[socco]]></dc:creator><pubDate>Sat, 30 Oct 2010 17:09:08 GMT</pubDate></item></channel></rss>