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 7508831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:44:55+00:00 2026-05-29T22:44:55+00:00

I have a C# application that needs to get variable-length data passed back from

  • 0

I have a C# application that needs to get variable-length data passed back from a C++ DLL. Typically I solve this problem as follows: for each entity type (such as a string or a bitmap) I have a persistent C++ variable of the appropriate type. The C# app then calls something like “QueryBitmapByName(string bitmapName)”, and the “contract” is that C# must process this variable before it calls another function in the C++ DLL. This has always worked in the past, but I have recently hit a stumbling block I don’t understand when returning multiple bitmaps. In Release mode the behavior is fine, but in Debug mode the bitmap passing fails to copy the pixel data correctly if the bitmaps are queried in rapid succession.

C# code fragment:

[StructLayout(LayoutKind.Sequential), Serializable]
public struct BCBitmapInfo
{
    [MarshalAs(UnmanagedType.U4)] public int width;
    [MarshalAs(UnmanagedType.U4)] public int height;
    [MarshalAs(UnmanagedType.SysInt)] public IntPtr colorData;
}

const string BaseCodeDLL = "BaseCode.dll";
[DllImport(BaseCodeDLL)] private static extern IntPtr BCQueryBitmapByName(IntPtr context, [In, MarshalAs(UnmanagedType.LPStr)] String bitmapName);

private Bitmap GetBitmap(String bitmapName)
{
    IntPtr bitmapInfoUnmanaged = BCQueryBitmapByName(baseCodeDLLContext, bitmapName);
    if (bitmapInfoUnmanaged == (IntPtr)0) return null;

    BCBitmapInfo bitmapInfo = (BCBitmapInfo)Marshal.PtrToStructure(bitmapInfoUnmanaged, typeof(BCBitmapInfo));

    return new Bitmap(bitmapInfo.width, bitmapInfo.height, bitmapInfo.width * 4, System.Drawing.Imaging.PixelFormat.Format32bppRgb, bitmapInfo.colorData);
}

private void UpdateReplayImages()
{
    pictureBoxSprites.Image = (Image)GetBitmap("replaySprites");
    pictureBoxTiles.Image = (Image)GetBitmap("replayTiles");
}

C++ Code fragment:

struct BCBitmapInfo
{
    UINT width;
    UINT height;
    BYTE *colorData;
};
class App
{
    ...
    BCBitmapInfo _queryBitmapInfo;
    Bitmap _queryBitmapDataA;
    Bitmap _queryBitmapDataB;
};

BCBitmapInfo* App::QueryBitmapByNameBad(const String &s)
{
    const Bitmap *resultPtr = &_queryBitmapDataA;

    if(s == "replayTiles") _queryBitmapDataA = MakeTilesBitmap();
    else if(s == "replaySprites") _queryBitmapDataA = MakeSpritesBitmap();

    _queryBitmapInfo.width = resultPtr->Width();
    _queryBitmapInfo.height = resultPtr->Height();
    _queryBitmapInfo.colorData = (BYTE*)resultPtr->Pixels();
    return &_queryBitmapInfo;
}

BCBitmapInfo* App::QueryBitmapByNameGood(const String &s)
{
    const Bitmap *resultPtr = NULL;

    if(s == "replayTiles")
    {
        resultPtr = &_queryBitmapDataA;
        _queryBitmapDataA = MakeTilesBitmap();
    }
    else if(s == "replaySprites")
    {
        resultPtr = &_queryBitmapDataB;
        _queryBitmapDataB = MakeSpritesBitmap();
    }

    _queryBitmapInfo.width = resultPtr->Width();
    _queryBitmapInfo.height = resultPtr->Height();
    _queryBitmapInfo.colorData = (BYTE*)resultPtr->Pixels();
    return &_queryBitmapInfo;
}

When running this code in Debug mode and using QueryBitmapByNameBad, whichever bitmap is queried first will have the correct dimensions but the pixel data will be all light green (the second bitmap will render fine); QueryBitmapByNameGood works fine because it uses a different container for each possible query. I don’t understand why QueryBitmapByNameBad doesn’t behave correctly in a single-threaded application: shouldn’t

new Bitmap(bitmapInfo.width, ..., bitmapInfo.colorData);

Be forced to copy the bitmapInfo.colorData out before it returns? Is it necessary to create a new “local storage container” for each variable-length query? Clearly this is complicated in the case of DLLs that can be called from multiple threads but for single-threaded the above approach seems like it should be sufficient.

  • 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-29T22:44:59+00:00Added an answer on May 29, 2026 at 10:44 pm

    The documentation makes it quite clear that the Bitmap will continue using that memory until it (the Bitmap object) is disposed:

    The caller is responsible for allocating and freeing the block of memory specified by the scan0 parameter. However, the memory should not be released until the related Bitmap is released.

    from Bitmap Constructor (Int32, Int32, Int32, PixelFormat, IntPtr)

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

Sidebar

Related Questions

I have an application that needs to hit the ActiveDirectory to get user permission/roles
We have an application that needs to know the path that it is executed
I have an application that needs to send a moderately high volume of messages
I have an application that needs to poll a webservice to see if the
we have an application that needs to send out various different types of template
I have an application that needs to work with a vendor-supplied API to do
I have an application that needs to check a website feed every second. Sometimes
I have an application that needs to highlight individual pixels on an image. I
I have an application that needs to determine whether a user has made a
I have an application that needs branding when downloaded in certain continents. When installed

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.