Click here to Skip to main content
15,906,569 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is the code

C#
int srcStride = bmData.Stride;
int dstStride = bmData.Stride;
int dst = bmData.Scan0.ToInt32() + dstStride * (height - 1);
int src = pBuffer.ToInt32();
for (int y = 0; y < height; y++)
{
    Win32.memcpy(dst, src, srcStride);
    dst -= dstStride;
    src += srcStride;
}


and it gives me an error in memcpy that reads this way
how can i fix that please?


A call to PInvoke function 'dshow!dshow.Core.Win32::memcpy' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature
Posted
Comments
Sergey Alexandrovich Kryukov 25-Jul-11 1:57am    
How did you obtain pBuffer?
--SA
john1990_1 25-Jul-11 2:03am    
// Callback method that receives a pointer to the sample buffer
public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
{
// create new image
System.Drawing.Bitmap img = new Bitmap(width, height, PixelFormat.Format24bppRgb);

// lock bitmap data
BitmapData bmData = img.LockBits(
new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);

// copy image data
int srcStride = bmData.Stride;
int dstStride = bmData.Stride;

int dst = bmData.Scan0.ToInt32() + dstStride * (height - 1);
int src = pBuffer.ToInt32();

for (int y = 0; y < height; y++)
{
Win32.memcpy(dst, src, srcStride);
dst -= dstStride;
src += srcStride;
}

// unlock bitmap data
img.UnlockBits(bmData);

// notify parent
parent.OnNewFrame(img);

// release the image
img.Dispose();

return 0;
}
dan!sh 25-Jul-11 2:11am    
1. What is Win32 class?
2. Where is the DllImport line of code?
john1990_1 25-Jul-11 2:16am    
namespace dshow.Core
{
using System;
using System.Runtime.InteropServices;

///
/// Some Win32 API functions
///

[ComVisible(false)]
public class Win32
{
// memcpy - copy a block of memery
[DllImport("ntdll.dll")]
public static extern
int memcpy(int dst, int src, int count);

// Supplies a pointer to an implementation of IBindCtx
[DllImport("ole32.dll")]
public static extern
int CreateBindCtx(
int reserved,
out UCOMIBindCtx ppbc);

// Converts a string into a moniker that identifies
// the object named by the string
[DllImport("ole32.dll", CharSet=CharSet.Unicode)]
public static extern
int MkParseDisplayName(
UCOMIBindCtx pbc,
string szUserName,
ref int pchEaten,
out UCOMIMoniker ppmk);

// window styles
[Flags]
public enum WS
{
CHILD = 0x40000000,
VISIBLE = 0x10000000
}
}
}

It looks like you need to learn a bit more on what .NET and CLR do and how it's different from native code.

This is managed platform, so you cannot interpret objects (I mean "object" in the most general sense of this word here, both reference and value objects, including primitive ones) as objects in real memory and perform direct operations with them by copying them in memory. You simply don't have direct access to memory.

I don't even ask how you P/Invoke memcpy — it does not matter. Basically, you have two ways to work with bitmap memory: you can fix memory and get access to memory pointers, which is only possible in unsafe mode, or you can use the method System.Runtime.InteropServices.Marshal.Copy to work with IntPtr Scan0.

Please see the code sample from the MSDN help page for System.Drawing.Bitmap.LockBits: http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx[^].

See also http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.aspx[^].

For using unsafe option of the .NET project and pointers for array copy operations, see http://msdn.microsoft.com/en-us/library/28k1s2k6.aspx[^].
See also for unsafe and pointers in .NET in general: http://msdn.microsoft.com/en-us/library/t2yzs44b.aspx[^].

—SA
 
Share this answer
 
Comments
john1990_1 25-Jul-11 2:24am    
can you correct my code, and give me a code that works on visual c# 2010 and i choose u best answer
Sergey Alexandrovich Kryukov 25-Jul-11 19:50pm    
There is nothing to fix. Also, you did not explain what you want to achieve, so fix what? The code sample is very close to what you should do, I suggest you look at it. It's easy to understand how it works.
--SA
I think you need to correct the declaration for memcpy method. It should be IntPtr and not int. Here[^] an example.
 
Share this answer
 
Comments
john1990_1 25-Jul-11 2:31am    
and then how to call the function memcpy
i made it like this

public static extern IntPtr memcpy(
IntPtr dst,
IntPtr src,
int count);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900