Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

Pinvoke question: How to structure callback that includes a buffer

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tom Allen - 29 Aug 2007 18:34 GMT
We are using a 3rd party API that processes video stream files to update a
data structure in the file.  I am writting a C# facade for this API but I am
having a hard time getting it to pass data correctly.  Can someone please
check out the approach I am using and give me some advice?

The API is structured where I initialize it by passing in two callback
methods.  These methods in my C# code are called by the API to read and write
buffers from the input or output files.  When I populate the "get" buffer in
the GetStreamBuf method I should see that same buffer in the subsequent
PutStreamBuf callback, but the PutStreamCallback appears to recieve an empty
buffer, as though the GetStreamBuf didn't really popultate the buffer.

//API Initialization
public delegate Int32 GetStreamBufCallback([In, Out] Byte[] Buf, Int32
buf_len);
public delegate Int32 PutStreamBufCallback([In, Out] Byte[] Buf, Int32
buf_len);

[DllImport("tableprocessor.dll", CharSet = CharSet.Ansi, EntryPoint = "#33",
CallingConvention=CallingConvention.StdCall)]
private static extern int InitProcessor(
GetStreamBufCallback GsCb,
PutStreamBufCallback PsCb,
Boolean enableLogging,
Int32 logLevel,
String logFile
);

//Callback methods
      System.IO.FileStream fsInput;
       Byte[] sharedGetBuf = new Byte[PACKET_SIZE];  //Allocate buffer
       Byte[] sharedPutBuf = new Byte[PACKET_SIZE];  //Allocate buffer

       public Int32 GetStreamBuf(Byte[] buf, Int32 bufSize)
       {
           if (fsInput != null)
           {
               if (fsInput.Read(sharedGetBuf, 0, bufSize) ==
TpFacade.PACKET_SIZE)
                   {
                   buf = sharedGetBuf;
                   return SUCCESS;                 //return callback status
to API
                   }
            }
         }

public Int32 PutStreamBuf(Byte[] buf, Int32 bufSize)
{
    sharedPutBuf = buf;
  //...
    return SUCCESS; //Success == 0

 }
Signature

Regards,
Tom Allen

Tom Allen - 29 Aug 2007 18:40 GMT
And the call to initialize the API
           if (TpFacade.InitProcessorFacade(GetStreamBuf, PutStreamBuf,
m_EnableLogging, m_LogLevel, apiLogFileString))
           { return; }

Thanks for any help!
Signature

Regards,
Tom Allen

> We are using a 3rd party API that processes video stream files to update a
> data structure in the file.  I am writting a C# facade for this API but I am
[quoted text clipped - 50 lines]
>  
>   }
Mattias Sjögren - 29 Aug 2007 20:35 GMT
Tom,

>//API Initialization
>public delegate Int32 GetStreamBufCallback([In, Out] Byte[] Buf, Int32
>buf_len);
>public delegate Int32 PutStreamBufCallback([In, Out] Byte[] Buf, Int32
>buf_len);

The runtime can't know how bit those buffers are unless you tell it.
Try adding the attribute [MarshalAs(UnmanagedType.LPArray,
SizeParamIndex=1)] to the Buf parameters.

>        public Int32 GetStreamBuf(Byte[] buf, Int32 bufSize)
>        {
[quoted text clipped - 4 lines]
>                    {
>                    buf = sharedGetBuf;

Here you're just changing which array the local buf parameter refers
to. It will not change the content of the buffer that is passed to
you. You have to copy the data into buf (or read into it directly)
instead.

Mattias

Signature

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

Tom Allen - 29 Aug 2007 21:48 GMT
Thanks, Mattias!  That helps a lot.  I'm not out of the woods, yet, but I'm
on my way... :)
Signature

Regards,
Tom Allen

> Tom,
>
[quoted text clipped - 23 lines]
>
> Mattias

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.