473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

__declspec(dllexport) to return char but errors in VB6

I have a C# class that I wan't to be able to use in VB6 and VBA
applications. To do this I was trying to use a mixed managed VC++ dll and
export a function. Doing this I get an error "The memory could not be
"read"".

Can any one explain what I am doing wrong?

This is my Code.
..CPP

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::InteropServices;
using namespace System::Collections;
using namespace WdCadTmInfo;
#define WDINFO_LINKAGE __declspec(dllexport)
#include "PlotAccounting-C.h"

__gc class ManagedObjects{
public:
};

BOOL APIENTRY
DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
{

return TRUE;
}

extern "C"{
WDINFO_LINKAGE char* getWdInfo(char* Param){

wdCadTeamInfo* tmpInfo = new wdCadTeamInfo();

IntPtr ptr = Marshal::StringToHGlobalAnsi(tmpInfo->GetWdCadTmInfo(Param));
char __nogc* pStr = static_cast<char*>(ptr.ToPointer());

Marshal::FreeHGlobal(ptr);

return pStr;

}
}

..H

#ifndef WDINFO_LINKAGE
#define WDINFO_LINKAGE __declspec(dllimport)
#endif
extern "C"{
WDINFO_LINKAGE char* getWdInfo(char* Param);
}
Nov 17 '05 #1
5 2200
>Can any one explain what I am doing wrong?

The pointer you're returning is invalid since you just freed the
memory it points to.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #2
Thank you for your reply!

I was switching code around and placed the FreeHGlobal in the wrong place
for my post.

changing it to this

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.

Thanks in advance.

Jason
Wood
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:u2**************@TK2MSFTNGP15.phx.gbl...
Can any one explain what I am doing wrong?


The pointer you're returning is invalid since you just freed the
memory it points to.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 17 '05 #3
>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.
Nov 17 '05 #4
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::InteropService s::Marshal::StringToHGloba
lAnsi(S"Return Value").ToPointer());
StrCpy(returnParm,pStr);

System::Runtime::InteropServices::Marshal::FreeCoT askMem(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" <ma********************@mvps.org> wrote in message
news:uI*************@tk2msftngp13.phx.gbl...
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.

Nov 17 '05 #5
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


"Jason W" <ja***@itguy.N.o.S.P.A.M.p.l.e.a.s.e.CC> wrote in message
news:Os**************@TK2MSFTNGP10.phx.gbl...
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::InteropService s::Marshal::StringToHGloba lAnsi(S"Return Value").ToPointer());
StrCpy(returnParm,pStr);

System::Runtime::InteropServices::Marshal::FreeCoT askMem(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" <ma********************@mvps.org> wrote in message
news:uI*************@tk2msftngp13.phx.gbl...
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.


Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cengiz | last post by:
Hi, i've created DLL with VC++ 6.0 with the following code. -------------------------------------------------- int __declspec(dllexport) initialize(void) { MessageBox(0,"TEST","TEST",0);...
3
by: Gawel | last post by:
Hajo, I have two dlls, both of them are compiled with /clr switch. In first dll project I have managed and unmanaged classes. One of the unmanaged I would like to use outside therefore I marked...
5
by: Felix I. Wyss | last post by:
Good Afternoon, I recently noticed that some very simple methods of a template declared and used in a DLL library get inlined when used by the DLL itself, but not by other DLLs and EXEs. After...
8
by: bonk | last post by:
Hello, I created a MFC extension dll (using VS 2005 Beta 2) that is supposed to export a class that uses .NET internally (See header below) und later shall be used by a plain MFC Project (without...
7
by: Martin Pritchard | last post by:
Hi, Sorry for my ignorance, but I'm a bit new to C++. I've been handed over a C++ app written in VS2002 which I have to convert to VS2005. Apparently it's been written in a C style, but cannot...
3
by: majestik666 | last post by:
Hi, i'm bulding a multi platform app under windows/linux/osx an i have a bit of trouble exporting c++ symbols from a dynamic library... Under windows, i compile a dll exporting symbols using :...
2
by: Luis | last post by:
Hello... How can I do an exported function in Visual Basic.Net? In Visual C++ the source is the next... but I need the same in VB.Net Thank you /* This is an example of an exported...
2
by: Bit Byte | last post by:
I have written a Dll which contains a class. I overload the "<<" opertor in global functions to perform Ostream operations - a follows: ostream &operator << (ostream &os, const struct _mystruct_t...
1
by: =?Utf-8?B?RmFiaWFu?= | last post by:
Hello, I want to give multiple native classes (in different mixed mode dlls) access to a managed output window (for error messages). Therefore I wrote a native singleton with __declspec...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.