Is it possible to convert C++ code into VBA language? If yes then how do I do this?
(I have no experience/knowledge of C++)
Is it possible to convert C++ code into VBA language? If yes then how do I do this?
(I have no experience/knowledge of C++)
*******************************************************
HELP WANTED! (Links to Forum threads)
Trying to create reusable code for Custom Events at Workbook (not Application) level
*******************************************************
Hi
http://www.tangiblesoftwaresolutions...r_Details.html
I have no idea if this works..
One test is worth a thousand opinions.
Click the * Add Reputation below to say thanks.
That's only for .net marvin. It depends on What the code does, you can do things in c++ that you can't in excel
OK. Do you think it would be possible to convert the C++ code found here:
http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
which I think is a function called from the C++ code here:
http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
If it helps, I'm not seeking to convert all the code on these two pages. I'm only interested in the portion that will: "get the size by passing the first 24 bytes of the Header Object to the IMFASFContentInfo::GetHeaderSize method"
I'm trying to convert it but it is not easy (seeing as I have never learnt C++). What is this hr that appears a lot? Is hr the function result?
![]()
Option Explicit '// goal = VBA function for obtaining WMA file object header size '// achieve this goal by converting C++ code into VBA! ' ' see: http:'//msdn.microsoft.com/en-us/library/windows/desktop/bb530124(v=vs.85).aspx ' N.B. we don't need to convert all of this C++ code. Only up to the end of part 3 of 7 'just comment below out? #include <stdio.h> '// Standard I/O #include <windows.h> '// Windows headers #include <mfapi.h> '// Media Foundation platform #include <wmcontainer.h> '// ASF interfaces #include <Mferror.h> #pragma comment(lib, "mfplat") #pragma comment(lib, "mf") #pragma comment(lib, "mfuuid") Function SafeRelease() 'should be a class in VBA? template <class T> void SafeRelease(T **ppT) { if (*ppT) then (*ppT)->Release() *ppT = NULL End If } End Function Function GetHeaderSize() As Long '// Read data from a byte stream into a media buffer. '// '// This function reads a maximum of cbMax bytes, or up to the size size of the '// buffer, whichever is smaller. If the end of the byte stream is reached, the '// actual amount of data read might be less than either of these values. '// '// To find out how much data was read, call IMFMediaBuffer::GetCurrentLength. HRESULT ReadFromByteStream( IMFByteStream *pStream, '// Pointer to the byte stream. IMFMediaBuffer *pBuffer, '// Pointer to the media buffer. DWORD cbMax '// Maximum amount to read. ) { DWORD cbBufferMax = 0 DWORD cbRead = 0 BYTE *pData= NULL HRESULT hr = pBuffer->Lock(&pData, &cbBufferMax, NULL) '// Do not exceed the maximum size of the buffer. If (SUCCEEDED(hr)) Then If (cbMax > cbBufferMax) Then cbMax = cbBufferMax End If '// Read up to cbMax bytes. hr = pStream->Read(pData, cbMax, &cbRead) End If '// Update the size of the valid data in the buffer. If (SUCCEEDED(hr)) Then hr = pBuffer->SetCurrentLength(cbRead) End If If (pData) Then pBuffer->Unlock() End If return hr } '// Read the ASF Header Object from a byte stream and return a pointer to the '// populated ASF ContentInfo object. '// '// The current read position of the byte stream must be at the start of the '// ASF Header Object. HRESULT CreateContentInfo(IMFByteStream *pStream, IMFASFContentInfo **ppContentInfo) { Const MIN_ASF_HEADER_SIZE As Long = 30 cbHeader = 0 cbBuffer = 0 IMFASFContentInfo *pContentInfo = NULL IMFMediaBuffer *pBuffer = NULL '// Create the ASF ContentInfo object. HRESULT hr = MFCreateASFContentInfo(&pContentInfo) '// Read the first 30 bytes to find the total header size. if (SUCCEEDED(hr)) { hr = MFCreateMemoryBuffer(MIN_ASF_HEADER_SIZE, &pBuffer); } if (SUCCEEDED(hr)) { hr = ReadFromByteStream(pStream, pBuffer,MIN_ASF_HEADER_SIZE); } if (SUCCEEDED(hr)) { hr = pContentInfo->GetHeaderSize(pBuffer, &cbHeader); } '// Pass the first 30 bytes to the ContentInfo object. If (SUCCEEDED(hr)) Then hr = pContentInfo->ParseHeader(pBuffer, 0) End If Call SafeRelease(&pBuffer) if (SUCCEEDED(hr) then cbBuffer = (DWORD)(cbHeader - MIN_ASF_HEADER_SIZE) hr = MFCreateMemoryBuffer(cbBuffer, &pBuffer) End If '// Read the rest of the header and finish parsing the header. If (SUCCEEDED(hr)) Then hr = ReadFromByteStream(pStream, pBuffer, cbBuffer); if (SUCCEEDED(hr)) { hr = pContentInfo->ParseHeader(pBuffer, MIN_ASF_HEADER_SIZE); } if (SUCCEEDED(hr)) { '// Return the pointer to the caller. *ppContentInfo = pContentInfo; (*ppContentInfo)->AddRef(); } SafeRelease(&pBuffer); SafeRelease(&pContentInfo); return hr; } End Function
It's an HRESULT which is a Long indicating the result of the function.
Everyone who confuses correlation and causation ends up dead.
what is DWORD? Is this a datatype? What is SUCCEEDED? It doesn't appear to be a function in the code so is it some C++ inbuilt function??
![]()
Option Explicit '// goal = VBA function for obtaining WMA file object header size '// achieve this goal by converting C++ code into VBA! ' ' see: http:'//msdn.microsoft.com/en-us/library/windows/desktop/bb530124(v=vs.85).aspx ' N.B. we don't need to convert all of this C++ code. Only up to the end of part 3 of 7 'just comment below out? #include <stdio.h> '// Standard I/O #include <windows.h> '// Windows headers #include <mfapi.h> '// Media Foundation platform #include <wmcontainer.h> '// ASF interfaces #include <Mferror.h> #pragma comment(lib, "mfplat") #pragma comment(lib, "mf") #pragma comment(lib, "mfuuid") Function SafeRelease() 'this should be a class in VBA, not a function? template <class T> void SafeRelease(T **ppT) { if (*ppT) then (*ppT)->Release() *ppT = NULL End If } End Function Function ReadFromByteStream( IMFByteStream *pStream, _ IMFMediaBuffer *pBuffer, _ DWORD cbMax _ ) as long '// IMFByteStream *pStream = Pointer to the byte stream. '// IMFMediaBuffer *pBuffer = Pointer to the media buffer. '// DWORD cbMax = Maximum amount to read. '// Read data from a byte stream into a media buffer. '// '// This function reads a maximum of cbMax bytes, or up to the size size of the '// buffer, whichever is smaller. If the end of the byte stream is reached, the '// actual amount of data read might be less than either of these values. '// '// To find out how much data was read, call IMFMediaBuffer::GetCurrentLength. Dim lngResult As Long 'dword = string? DWORD cbBufferMax = 0 DWORD cbRead = 0 BYTE *pData= NULL lngResult = pBuffer->Lock(&pData, &cbBufferMax, NULL) '// Do not exceed the maximum size of the buffer. If (SUCCEEDED(lngResult)) Then If (cbMax > cbBufferMax) Then cbMax = cbBufferMax End If '// Read up to cbMax bytes. lngResult = pStream->Read(pData, cbMax, &cbRead) End If '// Update the size of the valid data in the buffer. If (SUCCEEDED(lngResult)) Then lngResult = pBuffer->SetCurrentLength(cbRead) End If If (pData) Then pBuffer->Unlock() End If ReadFromByteStream = lngResult End Function Function CreateContentInfo(IMFByteStream *pStream, IMFASFContentInfo **ppContentInfo) as long '// Read the ASF Header Object from a byte stream and return a pointer to the '// populated ASF ContentInfo object. '// '// The current read position of the byte stream must be at the start of the '// ASF Header Object. Const MIN_ASF_HEADER_SIZE As Long = 30 cbHeader = 0 cbBuffer = 0 IMFASFContentInfo *pContentInfo = NULL IMFMediaBuffer *pBuffer = NULL '// Create the ASF ContentInfo object. lngResult = MFCreateASFContentInfo(&pContentInfo) '// Read the first 30 bytes to find the total header size. if (SUCCEEDED(lngResult)) { lngResult = MFCreateMemoryBuffer(MIN_ASF_HEADER_SIZE, &pBuffer); } if (SUCCEEDED(lngResult)) { lngResult = ReadFromByteStream(pStream, pBuffer,MIN_ASF_HEADER_SIZE); } if (SUCCEEDED(lngResult)) { lngResult = pContentInfo->GetHeaderSize(pBuffer, &cbHeader); } '// Pass the first 30 bytes to the ContentInfo object. If (SUCCEEDED(lngResult)) Then lngResult = pContentInfo->ParseHeader(pBuffer, 0) End If Call SafeRelease(&pBuffer) if (SUCCEEDED(lngResult) then cbBuffer = (DWORD)(cbHeader - MIN_ASF_HEADER_SIZE) lngResult = MFCreateMemoryBuffer(cbBuffer, &pBuffer) End If '// Read the rest of the header and finish parsing the header. If (SUCCEEDED(lngResult)) Then lngResult = ReadFromByteStream(pStream, pBuffer, cbBuffer); if (SUCCEEDED(lngResult)) { lngResult = pContentInfo->ParseHeader(pBuffer, MIN_ASF_HEADER_SIZE); } if (SUCCEEDED(lngResult)) { '// Return the pointer to the caller. *ppContentInfo = pContentInfo; (*ppContentInfo)->AddRef(); } SafeRelease(&pBuffer); SafeRelease(&pContentInfo); CreateContentInfo = lngResult End Function
This is a good list of types: http://codingdomain.com/visualbasic/win32api/datatypes/
I don't think SUCCEEDED is built-in but it just tests if the value passed to it is >= 0.
Thanks again Rory
Well the code has less red text these days (see below) but I'm still in over my head.
What is all the ->Lock ->Read etc?
They seem to have some connection to the IMFMediaBuffer and IMFByteStream. I'm not sure what these are either. After googling I'm now thinking these could be APIs? Can anyone please confirm/deny?
![]()
Option Explicit '// goal = VBA function for obtaining WMA file object header size '// achieve this goal by converting C++ code into VBA! ' ' see: http:'//msdn.microsoft.com/en-us/library/windows/desktop/bb530124(v=vs.85).aspx ' N.B. we don't need to convert all of this C++ code. Only up to the end of part 3 of 7 'just comment below out? '#include <stdio.h> '// Standard I/O '#include <windows.h> '// Windows headers '#include <mfapi.h> '// Media Foundation platform '#include <wmcontainer.h> '// ASF interfaces '#include <Mferror.h> ' '#pragma comment(lib, "mfplat") '#pragma comment(lib, "mf") '#pragma comment(lib, "mfuuid") 'APIs? Type IMFByteStream End Type Type IMFMediaBuffer End Type Function SafeRelease(ppt) 'maybe this should be a class in VBA, not a function? template <class T> void SafeRelease(T **ppT) If (ppt) Then (ppT)->Release() ppt = Null End If End Function Function ReadFromByteStream( _ pStream As IMFByteStream, _ pBuffer As IMFMediaBuffer, _ cbMax As Long _ ) As Long '// pStream = Pointer to the byte stream. '// pBuffer = Pointer to the media buffer. '// cbMax = Maximum amount to read. '// Read data from a byte stream into a media buffer. '// '// This function reads a maximum of cbMax bytes, or up to the size size of the '// buffer, whichever is smaller. If the end of the byte stream is reached, the '// actual amount of data read might be less than either of these values. '// '// To find out how much data was read, call IMFMediaBuffer::GetCurrentLength. Dim lngResult As Long Dim cbRead As Long Dim pData As Byte cbBufferMax = 0 cbRead = 0 pData = Null lngResult = pBuffer->Lock(pData, cbBufferMax, NULL) '// Do not exceed the maximum size of the buffer. If lngResult > 0 Then If cbMax > cbBufferMax Then cbMax = cbBufferMax End If '// Read up to cbMax bytes. lngResult = pStream->Read(pData, cbMax, cbRead) End If '// Update the size of the valid data in the buffer. If lngResult > 0 Then lngResult = pBuffer->SetCurrentLength(cbRead) End If If (pData) Then pBuffer->Unlock() End If ReadFromByteStream = lngResult End Function Function CreateContentInfo(pStream As IMFByteStream, _ pContentInfo As IMFASFContentInfo _ ) As Long '// Read the ASF Header Object from a byte stream and return a pointer to the '// populated ASF ContentInfo object. '// '// The current read position of the byte stream must be at the start of the '// ASF Header Object. Const MIN_ASF_HEADER_SIZE As Long = 30 Dim pBuffer As IMFMediaBuffer cbHeader = 0 cbBuffer = 0 pContentInfo = Null pBuffer = Null '// Create the ASF ContentInfo object. lngResult = MFCreateASFContentInfo(pContentInfo) '// Read the first 30 bytes to find the total header size. If lngResult > 0 Then lngResult = MFCreateMemoryBuffer(MIN_ASF_HEADER_SIZE, pBuffer) End If If lngResult > 0 Then lngResult = ReadFromByteStream(pStream, pBuffer, MIN_ASF_HEADER_SIZE) End If If lngResult > 0 Then lngResult = pContentInfo->GetHeaderSize(pBuffer, cbHeader) End If '// Pass the first 30 bytes to the ContentInfo object. If lngResult > 0 Then lngResult = pContentInfo->ParseHeader(pBuffer, 0) End If Call SafeRelease(pBuffer) If lngResult > 0 Then cbBuffer = CLng(cbHeader - MIN_ASF_HEADER_SIZE) lngResult = MFCreateMemoryBuffer(cbBuffer, pBuffer) End If '// Read the rest of the header and finish parsing the header. If lngResult > 0 Then lngResult = ReadFromByteStream(pStream, pBuffer, cbBuffer) If lngResult > 0 Then lngResult = pContentInfo->ParseHeader(pBuffer, MIN_ASF_HEADER_SIZE) End If If lngResult > 0 Then '// Return the pointer to the caller. pContentInfo = pContentInfo (pContentInfo)->AddRef() End If End If Call SafeRelease(pBuffer) Call SafeRelease(pContentInfo) CreateContentInfo = lngResult End Function
They are data types, not API functions.
They are currently shown as data types in VBA because i made a guess at the time as to what they were. So you are saying my first guess was right?
If they are data types then how can they can do things like?![]()
pBuffer->SetCurrentLength(cbRead)
How do I recreate these in VBA or am I unable to because they are data types native to C++?(pls don't laugh, I have never used C or C++ languages)
You can't just ignore this part of the code, it's probably one of the most important parts of the code.
![]()
'#include <stdio.h> '// Standard I/O '#include <windows.h> '// Windows headers '#include <mfapi.h> '// Media Foundation platform '#include <wmcontainer.h> '// ASF interfaces '#include <Mferror.h> '
If posting code please use code tags, see here.
They aren't declarations, they are compiler directives http://www.cplusplus.com/doc/tutorial/preprocessor/.
Last edited by Norie; 05-23-2014 at 07:39 AM.
Thanks for pointing me to this. However as I have no knowledge of C++, I'm finding the tutorial confusing.
Quote:What does it mean by 'headers'?Source file inclusion (#include)
This directive has been used assiduously in other sections of this tutorial. When the preprocessor finds an #include directive it replaces it by the entire content of the specified header or file. There are two ways to use #include:
12
#include <header>
#include "file"
In the first case, a header is specified between angle-brackets <>. This is used to include headers provided by the implementation, such as the headers that compose the standard library (iostream, string,...). Whether the headers are actually files or exist in some other form is implementation-defined, but in any case they shall be properly included with this directive.![]()
Unsolved. Closing thread because I only wanted to port C++ code to create one function in VBA.
Which I have worked out myself.
http://www.excelforum.com/excel-prog...structure.html
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks