Re: __declspec(dllexport) to return char but errors in VB6



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.


.



Relevant Pages

  • Re: Graph traversieren wie ein Petri-Netz
    ... @param strValue zu prüfender String ... public static final boolean empty{ ... @param strBuffValue zu prüfender StringBuffer ...
    (de.comp.lang.java)
  • Re: Hashtable question
    ... * @param modelName is a filename less the .pim extension where the ... public void load() throws IOException { ... public void add(String keyString, Item item) ... * @param keyString the string to lookup by. ...
    (comp.lang.java.programmer)
  • Re: which JAR to use ?
    ... scan for a string that leads into the piece you want with indexOf. ... public static String getWantedCurrencies throws IOException ... * @param result Results to save, ... public static String get(String websiteURL, String relativeURL, ...
    (comp.lang.java.programmer)
  • Re: Time - BuffererdReader takes read in data - store in hash Map TAKING FOREVER
    ... then convert to string, then parse it yourself. ... @param prefix ... @exception IOException ... public static byterawReadEntireFile (File fromFile) throws ...
    (comp.lang.java.programmer)
  • Problems sending email javamail
    ... private String stmpServer; ... @param mm MailMessage ... // Part two is attachments ... The method GetBCCis undefined for the type MailMessage ...
    (comp.lang.java.programmer)