473,468 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

LPCTSTR in DLLImport in C#

ORC
Hi,

I have been Googling a lot trying to solve following problem, but without
success, so now I hope someone here will be able to help me (I have done a
lot of DLLImports but never where I had to import a LPCTSTR ):

In a native DLL:
void TestFunc(LPCTSTR TestString)
{
TestString = TEXT("Text to transfer");
MessageBox(NULL, TestString, NULL, NULL); // Works fine - messagebox
shows correct text!
}

In C#:
[DllImport("Test.dll", EntryPoint="TestFunc", SetLastError=true)]
public static extern void TestFunc(StringBuilder TestString);

private void buttonOK_Click(object sender, System.EventArgs e)
{
StringBuilder MyString = new StringBuilder(50)
TestFunc(MyString);
MessageBox.Show(MyString); // Doesn't work - MyString is always empty
}

Thanks
Ole

Nov 16 '05 #1
1 9370
ORC
OK - found a solution:
It is neccesary to copy the text in the DLL like this:
wcscpy((unsigned short *)TestString, TEXT("Text to transfer");

Thanks,
Ole
"ORC" <or*@sol.dk> wrote in message
news:e9**************@TK2MSFTNGP09.phx.gbl...
Hi,

I have been Googling a lot trying to solve following problem, but without
success, so now I hope someone here will be able to help me (I have done a
lot of DLLImports but never where I had to import a LPCTSTR ):

In a native DLL:
void TestFunc(LPCTSTR TestString)
{
TestString = TEXT("Text to transfer");
MessageBox(NULL, TestString, NULL, NULL); // Works fine - messagebox
shows correct text!
}

In C#:
[DllImport("Test.dll", EntryPoint="TestFunc", SetLastError=true)]
public static extern void TestFunc(StringBuilder TestString);

private void buttonOK_Click(object sender, System.EventArgs e)
{
StringBuilder MyString = new StringBuilder(50)
TestFunc(MyString);
MessageBox.Show(MyString); // Doesn't work - MyString is always empty
}

Thanks
Ole

Nov 16 '05 #2

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

Similar topics

2
by: Tom | last post by:
Is there such a type in C#. I have a C++ application that uses LPCTSTR to convert a Long Pointer to a Constant String? Thanks Tom
1
by: ebobnar | last post by:
I need to call the function LoadImage which take a LPCTSTR argument to specify the path to the image to load. However, I need to create this path dynamically from user input by concatenating...
8
by: ppcdev | last post by:
Here's what I try : LPCTSTR tst = (LPCTSTR) (LPCWSTR) Marshal::StringToHGlobalUni(str); c:\MyNetPrj\Prj0001\stunt.cpp(244): error C2440: 'type cast' : cannot convert from 'System::IntPtr' to...
10
by: farseer | last post by:
How can i do this? i'd like to call the following code: .... string url = <my urld>; TCHAR* urlParams = GetParams( ); url.append( (char * ) urlParams ); GotoURL( ( LPCTSTR ) url ); ...
10
by: Lucy Ludmiller | last post by:
Anyone knows how to convert a LPCTSTR to an STL striung?. Can't seem to finda nyting (that dosen't blab on for several pages) on the net about how to do this
3
by: fooshm | last post by:
Hello, I'm trying to convert a String^ to LPCTSTR. When using : LPCTSTR str = static_cast<LPCTSTR>(Marshal::StringToHGlobalAnsi(gcString).ToPointer()); in order to convert how ever I'm...
14
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
I have seen the following function to convert from a System::String^ to a const wchar_t*. I would like to get a LPCTSTR and AFAIK LPCTSTR is equal to const wchar_t*. Then it should all work right?...
7
by: justin001 | last post by:
In my _UNICODE & _MBCS build project, I have LPCTSTR string (i.e.it will be wchar_t*) .The following function is not giving proper result and instead it is producing junk data. I have confirmed it...
4
by: gw7rib | last post by:
I'm using a system in which TCHAR is typedef-ed to represent a character, in this case a wchar_t to hold Unicode characters, and LPCTSTR is typedef-ed to be a pointer to constant wchar_t. I presume...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.