Click here to Skip to main content
15,888,803 members
Home / Discussions / C#
   

C#

 
AnswerRe: DataGridView Pin
Imran Khan Pathan17-Sep-07 2:00
Imran Khan Pathan17-Sep-07 2:00 
QuestionSearch GUI to configurate Log4Net! Pin
T-Bear198716-Sep-07 23:25
T-Bear198716-Sep-07 23:25 
QuestionIntractivity with ms word?? Pin
Muammar©16-Sep-07 22:59
Muammar©16-Sep-07 22:59 
AnswerRe: Intractivity with ms word?? Pin
Pete O'Hanlon16-Sep-07 23:20
mvePete O'Hanlon16-Sep-07 23:20 
GeneralRe: Intractivity with ms word?? Pin
Muammar©17-Sep-07 0:55
Muammar©17-Sep-07 0:55 
QuestionWriting into an unmanaged buffer and marshalling it Pin
Leonardo Pelisoli16-Sep-07 22:46
Leonardo Pelisoli16-Sep-07 22:46 
GeneralBump Pin
Leonardo Pelisoli19-Sep-07 11:59
Leonardo Pelisoli19-Sep-07 11:59 
AnswerRe: Writing into an unmanaged buffer and marshalling it Pin
Leonardo Pelisoli21-Sep-07 5:37
Leonardo Pelisoli21-Sep-07 5:37 
Hi,

I found a solution to the problem. I'm not sure if it's the best one, since I'm forced to use unsafe code, but it works. The code example I give below is a variation of the one in my previous post. To show that the mechanism works, I simply write the input buffer into the output buffer.

Thanks anyway to anyone who might have spent time looking for a solution.

C#
using System;
using System.Runtime.InteropServices;
using System.Globalization;

namespace CsharpManaged
{
    class Program
    {
        public delegate void CallbackDelegate(
            IntPtr INBuffer, UInt32 INBufferLength,
            IntPtr OUTBuffer, UInt32 OUTBufferLength);

        [DllImport("CppDLLUnmanaged.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern void SetCallBackPointer(CallbackDelegate DelegatePassedToDLL);

        [DllImport("CppDLLUnmanaged.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern void InvokeCallBack();

        static void Main(string[] args)
        {
            SetCallBackPointer(new CallbackDelegate(CallbackFunction));
            InvokeCallBack();

            Console.ReadLine();
        }

        private static void CallbackFunction(
            IntPtr INBuffer, UInt32 INBufferLength,
            IntPtr OUTBuffer, UInt32 OUTBufferLength)
        {
            // For the example, we'll copy the IN buffer into the OUT buffer.
            // We'll also show it on screen
            Console.WriteLine("C#:");
            unsafe
            {
                Byte* INptr = (Byte*)INBuffer.ToPointer();
                Byte* OUTptr = (Byte*)OUTBuffer.ToPointer();
                // Truncating the biggest buffer (we don't want to write into non
                // allocated memory).
                UInt32 length =
                    (INBufferLength < OUTBufferLength) ? INBufferLength : OUTBufferLength;
                for (UInt32 i = 0; i < length; ++i)
                {
                    Console.WriteLine(Convert.ToString(*INptr));
                    *OUTptr = *INptr;
                    ++INptr;
                    ++OUTptr;
                }
            }
            Console.WriteLine();
        }
    }
}


C++ DLL
#include <iostream>
using namespace std;

// This is the prototype of the callback function
typedef void __stdcall CALL_BACK_FUNCTION(char* INBuffer, unsigned long INBufferLength,
										  char* OUTBuffer, unsigned long OUTBufferLength);

// This is only function that actually has to be exported. The caller will pass a pointer
// to the function it wants to use as callback.
CALL_BACK_FUNCTION *pFunction;
__declspec(dllexport) void __stdcall SetCallBackPointer(CALL_BACK_FUNCTION* FnAddress)
{
	pFunction = FnAddress;
}

// For the example, this is the function that will activate the
// callback. It won't exist in practice.
__declspec(dllexport) void __stdcall InvokeCallBack()
{
	// Allocating memory for the buffers
	char* INBuffer, * OUTBuffer;
	unsigned long INBufferLength, OUTBufferLength;

	INBufferLength = 10;
	OUTBufferLength = 15;

	INBuffer  = new char[INBufferLength];
	OUTBuffer = new char[OUTBufferLength];
	////////////////////////////////////////////////////////////
	
	// Filling the buffer with binary data, just for the example.
	for(int i = 0; i < INBufferLength; ++i)
	{
		INBuffer[i] = i;
	}
	////////////////////////////////////////////////////////////

	// Making the callback
	pFunction(INBuffer,INBufferLength,OUTBuffer,OUTBufferLength);
	////////////////////////////////////////////////////////////

	// Showing the return buffer contents, to show that the
	// transfer took place as expected.
	cout << "C++" << endl;
	for(unsigned long i = 0; i < OUTBufferLength; ++i)
	{
		char digit;
		itoa(OUTBuffer[i],&digit,10);
		cout << digit << endl;
	}
	////////////////////////////////////////////////////////////

	// Freeing allocated memory
	delete [] INBuffer;
	delete [] OUTBuffer;
	////////////////////////////////////////////////////////////
}

QuestionTo copy file from server..... Pin
P_Elza16-Sep-07 22:46
P_Elza16-Sep-07 22:46 
AnswerRe: To copy file from server..... Pin
Pete O'Hanlon16-Sep-07 23:17
mvePete O'Hanlon16-Sep-07 23:17 
QuestionRegarding Application Pool Pin
ngrj16-Sep-07 22:02
ngrj16-Sep-07 22:02 
AnswerRe: Regarding Application Pool Pin
Pete O'Hanlon17-Sep-07 1:45
mvePete O'Hanlon17-Sep-07 1:45 
GeneralRe: Regarding Application Pool Pin
ngrj17-Sep-07 17:05
ngrj17-Sep-07 17:05 
Questionstreamwriter Pin
troubled one16-Sep-07 21:56
troubled one16-Sep-07 21:56 
AnswerRe: streamwriter Pin
Pete O'Hanlon16-Sep-07 22:33
mvePete O'Hanlon16-Sep-07 22:33 
GeneralRe: streamwriter Pin
troubled one17-Sep-07 21:45
troubled one17-Sep-07 21:45 
AnswerRe: streamwriter Pin
N a v a n e e t h16-Sep-07 23:12
N a v a n e e t h16-Sep-07 23:12 
GeneralRe: streamwriter Pin
Giorgi Dalakishvili16-Sep-07 23:27
mentorGiorgi Dalakishvili16-Sep-07 23:27 
GeneralRe: streamwriter Pin
N a v a n e e t h16-Sep-07 23:37
N a v a n e e t h16-Sep-07 23:37 
AnswerRe: streamwriter Pin
blackjack215017-Sep-07 1:14
blackjack215017-Sep-07 1:14 
QuestionReducing the encrypted text size. Pin
M. J. Jaya Chitra16-Sep-07 21:33
M. J. Jaya Chitra16-Sep-07 21:33 
AnswerRe: Reducing the encrypted text size. Pin
Sathesh Sakthivel16-Sep-07 21:52
Sathesh Sakthivel16-Sep-07 21:52 
GeneralRe: Reducing the encrypted text size. Pin
M. J. Jaya Chitra16-Sep-07 22:13
M. J. Jaya Chitra16-Sep-07 22:13 
AnswerRe: Reducing the encrypted text size. Pin
Colin Angus Mackay16-Sep-07 21:54
Colin Angus Mackay16-Sep-07 21:54 
GeneralRe: Reducing the encrypted text size. Pin
M. J. Jaya Chitra17-Sep-07 0:02
M. J. Jaya Chitra17-Sep-07 0:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.