From: Felix on
Hello NG,

i ran into a new problem. I want to call a function from my dll,
which i want to load at runtime. But i need to add a CString to the
function i call.

My first attempt to do it without a CString works:

---

FARPROC pFunc;

HMODULE hMod = LoadLibrary(my.dll); //load DLL

pFunc = GetProcAddress(hMod, "myfunc");

if (pFunc)

(*pFunc)();

---

But how can i define and call it, if the dll-function looks like:

void myfunc(CString xyz); //??


From: David Wilkinson on
Felix wrote:
> Hello NG,
>
> i ran into a new problem. I want to call a function from my dll,
> which i want to load at runtime. But i need to add a CString to the
> function i call.
>
> My first attempt to do it without a CString works:
>
> ---
>
> FARPROC pFunc;
>
> HMODULE hMod = LoadLibrary(my.dll); //load DLL
>
> pFunc = GetProcAddress(hMod, "myfunc");
>
> if (pFunc)
>
> (*pFunc)();
>
> ---
>
> But how can i define and call it, if the dll-function looks like:
>
> void myfunc(CString xyz); //??

Felix:

Try this

typedef void (*MY_FUNC)(CString);
MY_FUNC pFunc = (MY_FUNC)GetProcAddress(hMod, "myfunc");

CString str = _T("xyz");
(*pFunc)(str);

--
David Wilkinson
Visual C++ MVP
From: Nick Meyer on
"Felix" wrote:

> Hello NG,
>
> i ran into a new problem. I want to call a function from my dll,
> which i want to load at runtime. But i need to add a CString to the
> function i call.
>
> My first attempt to do it without a CString works:
>
> ---
>
> FARPROC pFunc;
>
> HMODULE hMod = LoadLibrary(my.dll); //load DLL
>
> pFunc = GetProcAddress(hMod, "myfunc");
>
> if (pFunc)
>
> (*pFunc)();
>
> ---
>
> But how can i define and call it, if the dll-function looks like:
>
> void myfunc(CString xyz); //??
>
>
>

Hi Felix,

I believe the syntax is something like this:

typedef void (*MyFunc)(CString);

MyFunc pFunc;

CString str("a string");

HMODULE hMod = LoadLibrary("my.dll");

pFunc = GetProcAddress(hMod, "myfunc");

if (pFunc)

(*pFunc)(str);

Hope that's helpful.
From: Giovanni Dicanio on

"Felix" <nmc-clan(a)gmx.de> ha scritto nel messaggio
news:erZixLQsIHA.4544(a)TK2MSFTNGP04.phx.gbl...

> i ran into a new problem. I want to call a function from my dll,
> which i want to load at runtime. But i need to add a CString to the
> function i call.
>

> But how can i define and call it, if the dll-function looks like:
>
> void myfunc(CString xyz); //??

Your function prototype suggest that your CString parameter is an input
parameter.
So, I would pass it by const reference:

void myfunc( const CString & xyz );

or better, I would just pass a LPCTSTR, which IMHO is better in these
C-interface DLLs:

void myfunc( LPCTSTR xyz );

Of course you can have CString instances inside your code - CString plays
well with LPCTSTR (there is an implicit conversion - whenever you read
LPCTSTR you can pass a CString instance there, because a conversion operator
will be called to convert from CString to LPCTSTR).

Giovanni


From: Joseph M. Newcomer on
See below...
On Thu, 8 May 2008 14:19:57 +0200, "Felix" <nmc-clan(a)gmx.de> wrote:

>Hello NG,
>
>i ran into a new problem. I want to call a function from my dll,
>which i want to load at runtime. But i need to add a CString to the
>function i call.
>
>My first attempt to do it without a CString works:
>
>---
>
>FARPROC pFunc;
****
This is a generic type. The answer of declaring a typedef is part of the answer. However,
you will need to do a cast, e.g., taking Giovanni's observation into account, you would
write

typedef void (*MyFuncType)(const CString &);
MyFuncType pFunc;

but see below...
****
>
>HMODULE hMod = LoadLibrary(my.dll); //load DLL
>
>pFunc = GetProcAddress(hMod, "myfunc");
****
What was missing was the cast, e.g.,
pFunc = (MyFuncType)GetProcAddress(hMod, "myfunc"); // not _T()

Note there is a cast, and an annotation that the parameter is an 8-bit character string.
This is the only API I am aware of that takes a string but does not have -A and -W
variants; for any other API you would write

result = SomeAPI(thing, value, _T("SomeStringHere"));

but for this one, using the _T() notation would generate an error when you did a Unicode
compilation. I like to make this explicit.
****
>
>if (pFunc)
>
>(*pFunc)();
****
This notation is obsolete. It is sufficient to write
pFunc(str);
as there is no need to dereference the pointer explicitly; this is old K&R cruft, but
ISO/ANSI C automatically dereferences a function pointer.
joe
****
>
>---
>
>But how can i define and call it, if the dll-function looks like:
>
>void myfunc(CString xyz); //??
>
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm