
Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
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);
}
> >WDINFO_LINKAGE char* getWdInfo(char* Param){
> >
[quoted text clipped - 21 lines]
>
> Mattias
Jason W - 27 Jul 2005 21:29 GMT
I've found what I'm doing wrong. After reading a little feather in to StrCpy
I foud that I'm causing a buffer overrun by coping more than the origial
has. I need to test for the lenght before I copy the data.
Thanks for all your help Mattias!
Jason
Wood
> 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.
[quoted text clipped - 45 lines]
> 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;
[quoted text clipped - 36 lines]
> >
> > Mattias