Re: __declspec(dllexport) to return char but errors in VB6
- From: "Jason W" <jason@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 27 Jul 2005 16:14:42 -0400
I didn't think I was doing it correct. I've been reading and found what I
believe to be the correct way to do this but VB6 still errors.
I can get it to work in C# but that isn't the language I'm looking for.
Am I making since about what I'm asking for?
<C# Code>
[DllImport(@"PlotAccounting-C.dll")]
static extern long getWdInfo(
string param,
System.Text.StringBuilder returnParm
);
[STAThread]
static void Main(string[] args)
{
System.Text.StringBuilder returnIt = new
System.Text.StringBuilder(500,500);
Console.WriteLine(getWdInfo("Testing", returnIt).ToString());
Console.WriteLine(returnIt.ToString());
}
<VB6 Code>
Public Declare Function getWdInfo Lib "PlotAccounting-C.dll" (ByVal param As
String, paramOut As String) As Long
Dim testing As String
testing = "Not Set"
MsgBox getWdInfo("PlotAccounting", testing)
<PlotAccount-c.cpp>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#using <mscorlib.dll>
#define WDINFO_LINKAGE __declspec(dllexport)
#include "PlotAccounting-C.h"
#include "shlwapi.h"
__gc class ManagedObjects{
public:
};
BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
{
return TRUE;
}
extern "C"{
WDINFO_LINKAGE void getWdInfo(char* Param, LPTSTR returnParm){
char __nogc* pStr =
static_cast<char*>(System::Runtime::InteropServices::Marshal::StringToHGloba
lAnsi(S"Return Value").ToPointer());
StrCpy(returnParm,pStr);
System::Runtime::InteropServices::Marshal::FreeCoTaskMem(System::IntPtr((voi
d*)pStr));
return;
}
}
<PlotAccounting-c.h>
#ifndef WDINFO_LINKAGE
#define WDINFO_LINKAGE __declspec(dllimport)
#endif
extern "C"{
WDINFO_LINKAGE void getWdInfo(char* Param, LPTSTR returnKey);
}
"Mattias Sjögren" <mattias.dont.want.spam@xxxxxxxx> wrote in message
news:uIgQT7rkFHA.576@xxxxxxxxxxxxxxxxxxxxxxx
> >WDINFO_LINKAGE char* getWdInfo(char* Param){
> >
> > IntPtr ptr = Marshal::StringToHGlobalAnsi(S"Testing return");
> > char __nogc* pStr = static_cast<char*>(ptr.ToPointer());
> >
> > return pStr;
> >
> > Marshal::FreeHGlobal(ptr);
> >}
> >
> >doesn't give me the error but doesn't return anything back.
>
>
> Now the FreeHGlobal call is dead code that will never execute instead,
> the compiler should warn you about that.
>
> You either have to switch to a model where the caller passes in a
> buffer to be filled, specify that the caller must free the memory with
> LocalFree, or provide another function that frees the memory later.
> You can't both return the pointer and free the memory it points to in
> the same function.
>
> As to why you don't get anything on the VB side, I would have to see
> the callng code to tell.
>
>
>
> Mattias
>
> --
> Mattias Sjögren [MVP] mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.
.
- Follow-Ups:
- References:
- __declspec(dllexport) to return char but errors in VB6
- From: Jason W
- Re: __declspec(dllexport) to return char but errors in VB6
- From: Mattias Sjögren
- Re: __declspec(dllexport) to return char but errors in VB6
- From: Jason W
- Re: __declspec(dllexport) to return char but errors in VB6
- From: Mattias Sjögren
- __declspec(dllexport) to return char but errors in VB6
- Prev by Date: RE: strange pointer behavior
- Next by Date: Re: using a function pointer in another function
- Previous by thread: Re: __declspec(dllexport) to return char but errors in VB6
- Next by thread: Re: __declspec(dllexport) to return char but errors in VB6
- Index(es):
Relevant Pages
|