Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 591403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:37:52+00:00 2026-05-13T15:37:52+00:00

I am working on c# application(Using win 32 dll in my apllication)…I am trying

  • 0

I am working on c# application(Using win 32 dll in my apllication)…I am trying something like this
In DLL(test.dll):

char* Connect(TCHAR* lpPostData)
{
char buffer[1000];
.....
return buffer;
}

IN c# application:

[DllImport("test.dll", EntryPoint = "Connect", CharSet = CharSet.Unicode)]
 [return: MarshalAs(UnmanagedType.LPWStr)]
 public static extern string Connect(StringBuilder postdata);

string returnedData = Connect(postdata);

But returning of data is not happening properly….
Pls can any body tell where i am going wrong
Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:37:52+00:00Added an answer on May 13, 2026 at 3:37 pm

    TCHAR * in tells me it’s a unicode input (UNICODE is defined under CE), but the char * tells me it’s likely a byte array (or ascii string) and whoever created this API should be slapped for mixing the two, as it’s really, really poor design.

    You certainly can’t marshal the return as a wide character string, because it isn’t one. On the desktop you’d use Tony’s suggestion, but MSDN (and practice) clearly shows that it’s not available in the CF (no idea why MS thought we wouldn’t need it).

    The Smart Device Framework does have it. The other option is to use Marshal to copy from the returned pointer to a byte array and then use Encoding.ASCII to turn that array into a string. Of course that points out the other obvious flaw in this API that it shouldn’t be returning a string in the first place.

    EDIT 1

    Since I’m seeing other suggestions on what you should do that I don’t really agree with, I suppose I should give you an example:

    Your native call should look more like this:

    extern "C" 
    __declspec(dllexport) 
    const BOOL __cdecl Connect(TCHAR* lpPostData, 
                             TCHAR *returnBuffer, 
                             DWORD *returnSize) 
    { 
      // validate returnSize, returnBuffer, etc
      // write your data into returnBuffer
    
      TCHAR *data = _T("this is my data");
    
      _tcscpy(returnBuffer, data);
      *returnSize = (_tcslen(data) + 1) * sizeof(TCHAR);
    
      return succeeded;
    } 
    

    Note that I’m simply returning a success code. The text data is passed in as a pointer, along with a length for it (so the API knows how much space it can use and can return how much it did use). Also not that I’m consistent with my string variable data types, and I’m using the TCHAR macros which will become wchar_t under CE, which is consistent with the rest of the OS (which has almost no ASCII APIs to begin with).

    Most of the WIn32 API set works this exact same way.

    Your P/Invoke declaration, then is very simple:

    [DllImport("test.dll", SetLastError=true)] 
    private static extern bool Connect(string postData, 
                                       StringBuilder data, 
                                       ref int length);
    

    Using it is also straightforward:

    void Foo()
    {
      int length = 260;
      StringBuilder sb = new StringBuilder(length);
      if(Connect("Bar", sb, ref length))
      {
        // do something useful
      }
    } 
    

    Note that the StringBuilder has to get initialized to some size, and that size is what you pass in fore the third parameter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 292k
  • Answers 292k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you use the magic method __invoke, you can call… May 13, 2026 at 6:12 pm
  • Editorial Team
    Editorial Team added an answer Sure. Heck, you can replace arithmetic with LINQ queries. But… May 13, 2026 at 6:12 pm
  • Editorial Team
    Editorial Team added an answer jQuery: $(function(){ $(".comments").not(":first").hide(); $("#btnViewAll").click(function(){ $(".comments").slideDown(); }); }); HTML: <input type="button"… May 13, 2026 at 6:12 pm

Related Questions

I have a .NET project that's always been built/run by/on 32 bit machines. I
I am working on a c++ win32 program that involves a keyboard hook. The
I am working on web application .net 3.5,asp.net,C#. In that i need to provide
Background: I am writing a C++ program working with large amounts of geodata, and
I am working on C++ and COM/ATL in Windows from last few years. Now

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.