cancel
Showing results for 
Search instead for 
Did you mean: 

Basic samples code in C++

Former Member
0 Kudos

Hello everyone. I'm in the middle of taking my SAP SDK certification and I'm having problems making the examples work. I’m a C++ developer so naturally I would like to write in C+. I was wondering if someone could make some basic sample code (DI and UI connection code would be fine.) I found the C+ samples on the smb portal but it is to comprehensive.

Accepted Solutions (0)

Answers (9)

Answers (9)

Former Member
0 Kudos

Thanks to all you ho have helped me in getting started with the SDK.

SAP portal teem

Gali Moualem (SAP)

Orly Amrany (SAP)

Trond Pedersen (SAP Norway)

Miki Zilbershtein (SAP)

Ivar Just Olsen (Ho knows everything about programming)

Yaniv Gamliel(SAP)

The people ho wrote the BE_UI_Calculator and the other c++ samples (I cant find

you names)

The msdn

And to everyone else ho is contributing to the forum

Stay tuned

More samples will follow soon.

Former Member
0 Kudos

// this example is based on the microsoft example http://msdn.microsoft.com/libr

ary/default.asp?url=/library/en-us/xmlsdk/html/dom_hdi_cpp_loaddom.asp

//Sorry about not cleaning up all the Microsoft error testing stuff to make the

code more readable as this is sample code only

#include <tchar.h>

#import "C:\Program Files\SAP Manage\SAP Business One\SAPbouiCOM.exe"

//using namespace SAPbouiCOM; //Im not to fond of using namespaces thats your choice.

#import <msxml3.dll> raw_interfaces_only

//using namespace MSXML2;

// Macro that calls a COM method returning HRESULT value:

#define HRCALL(a, errmsg) \

do { \

hr = (a); \

if (FAILED(hr)) { \

goto clean; \

} \

} while (0)

// Helper function to create a DOM instance:

MSXML2::IXMLDOMDocument * DomFromCOM()

{

HRESULT hr;

MSXML2::IXMLDOMDocument *pxmldoc = NULL;

HRCALL( CoCreateInstance(__uuidof(MSXML2::DOMDocument30),

NULL,

CLSCTX_INPROC_SERVER,

__uuidof(MSXML2::IXMLDOMDocument),

(void**)&pxmldoc),

"Create a new MSXML2::DOMDocument");

HRCALL( pxmldoc->put_async(VARIANT_FALSE),

"should never fail");

HRCALL( pxmldoc->put_validateOnParse(VARIANT_FALSE),

"should never fail");

HRCALL( pxmldoc->put_resolveExternals(VARIANT_FALSE),

"should never fail");

return pxmldoc;

clean:

if (pxmldoc)

{

pxmldoc->Release();

}

return NULL;

}

//main

int APIENTRY _tWinMain(HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPTSTR lpCmdLine,

int nCmdShow)

{

//int hr;

// declare a SboGuiApi

SAPbouiCOM::ISboGuiApiPtr oGui;

// decalre a Application

SAPbouiCOM::IApplicationPtr oApp;

MSXML2::IXMLDOMDocument *pXMLDom=NULL;

MSXML2::IXMLDOMParseError *pXMLErr=NULL;

BSTR bstr = NULL;

VARIANT_BOOL status;

VARIANT var;

HRESULT hr;

CoInitialize (NULL); //Init COM Lib Dll's

pXMLDom = DomFromCOM();

if (!pXMLDom) goto clean;

hr = oGui.CreateInstance ("SAPbouiCOM.SboGuiApi");

if (FAILED(hr))

{

return FALSE; // the create dispatch method failed so return FAL

SE

}

oGui->Connect ("0030002C0030002C00530041005000420044005F0044006100740065

0076002C0050004C006F006D0056004900490056");

// get the recent application

oApp = oGui->GetApplication(0);

oApp->MessageBox("Got Application object", 1, "", "", "");

VariantInit(&var);

V_BSTR(&var) = SysAllocString(L"MySimpleForm.xml");

V_VT(&var) = VT_BSTR;

HRCALL(pXMLDom->load(var, &status), "");

if (status!=VARIANT_TRUE) {

HRCALL(pXMLDom->get_parseError(&pXMLErr),"");

HRCALL(pXMLErr->get_reason(&bstr),"");

//dprintf("Failed to load DOM from stocks.xml. %S\n",bstr);

goto clean;

}

HRCALL(pXMLDom->get_xml(&bstr), "");

//dprintf("XML DOM loaded from stocks.xml:\n%S\n",bstr);

oApp->LoadBatchActions(&bstr);

clean:

if (bstr) SysFreeString(bstr);

if (&var) VariantClear(&var);

if (pXMLErr) pXMLErr->Release();

if (pXMLDom) pXMLDom->Release();

CoUninitialize();

return 0;

}

Former Member
0 Kudos

A single sign-on example, Hope this will help someone:)

***********************************

The includes

***********************************

#import "C:\Program Files\SAP Manage\SAP Business One\SAPbouiCOM.exe"

//using namespace SAPbouiCOM; //Im not to fond of using

#import "C:\Program Files\SAP Manage\SAP Business One DI API\DI API 65\Object\SA

PbobsCOM65.dll" rename_namespace ("SAPbobsCOM")

***********************************

The code

***********************************

//declearations

int hr;

BSTR sCookie;

BSTR sConnectionContext;

//std::basic_string <char> sCookie;

//std::basic_string <char> sConnectionContext;

CoInitialize(NULL); //Init COM Lib Dll's

// declare SboGuiApi

SAPbouiCOM::ISboGuiApiPtr oGui;

// decalre Application

SAPbouiCOM::IApplicationPtr oApp;

// decaler Company

SAPbobsCOM::ICompanyPtr oCompany("SAPbobsCOM.Company");// company pointe

r

//initializing ui

hr = oGui.CreateInstance ("SAPbouiCOM.SboGuiApi");

if (FAILED(hr))

{

return FALSE; // the create dispatch method failed so return FAL

SE

}

//initializing di

hr = oCompany.CreateInstance ("SAPbobsCOM.Company");

if (FAILED(hr))

{

return FALSE; // the create dispatch method failed so return FAL

SE

}

//Connecting to the gui

oGui->Connect ("0030002C0030002C00530041005000420044005F0044006100740065

0076002C0050004C006F006D0056004900490056");

//get the application

oApp = oGui->GetApplication(0);

oApp->MessageBox("GetApplication worked", 1, "", "", "");

//Get the context cookie

sCookie = oCompany->GetContextCookie();

//get the connection context

sConnectionContext = oApp->Company->GetConnectionContext(sCookie);

//Disconcet if the company is conceted

if (oCompany->Connected)

{

oCompany->Disconnect();

}

oCompany->SetSboLoginContext(sConnectionContext);

//oCompany->Connect();

hr = oCompany->Connect();

if (hr == 0)

{

oApp->MessageBox((_bstr_t)"Connection ot company object Success!

\n You are now connected to the company: " + oCompany->CompanyName + (_bstr_t)"\

n The single sign-on worked!", 1, "", "", "");

}

else

{

oApp->MessageBox("Connection to company object failed!", 1, "", "", "");

}

Former Member
0 Kudos

Hi All,

I have found in sap servicemarketplace / smb a sample C++ programs. (go solution development/SDK/Downloads)

in samples section Download C++ 6.0 Sample Programs release 6.2 it also works with 7.6

Open the calculator sample and replace in CBE_UI_CalculatorDlg class on the

CBE_UI_CalculatorDlg::OnInitDialog() function the getapplication with the following

con = gui->GetApplication(-1L);

It works.

by Janos

Former Member
0 Kudos

Could anyone post a C/C++ code snippet that has an event handler in it as well?

Thanks

Adam

Former Member
0 Kudos

//a create menu item example.

//some declarations

IMenusPtr oMenus;

IMenuItemPtr oMenuItem;

IMenuCreationParamsPtr oCreationPackage;

oMenus = oApp->Menus;

//Removing menu if it exists

if (oMenus->Exists("MyMenu01"))

{

oMenus->RemoveEx("MyMenu01");

}

//sPaht = Application::StartupPath;

oMenuItem = oMenus->Item(_variant_t("43520"));

oCreationPackage = oApp->CreateObject(cot_MenuCreationParams);

oCreationPackage->Type = mt_POPUP;

oCreationPackage->UniqueID = "MyMenu01";

oCreationPackage->String = "Sample Menu";

//oCreationPackage->Enabled = true;

//oCreationPackage->Image = "UI.bmp";

//oCreationPackage->Image = sPath & "UI.bmp";

oCreationPackage->Position = 15;

oMenus = oMenuItem->SubMenus;

oMenus->AddEx(oCreationPackage);

oMenuItem = oApp->Menus->Item("MyMenu01");

oMenus = oMenuItem->SubMenus;

// Create s sub menu

oCreationPackage->Type = mt_STRING;

oCreationPackage->UniqueID = "MySubMenu";

oCreationPackage->String = "Sample Sub Menu";

oMenus->AddEx(oCreationPackage);

Former Member
0 Kudos

//Here is a simple form example.

// 03.SimpleForm.cpp

#pragma once

#include <iostream>

#include <tchar.h>

#import "C:\Program Files\SAP Manage\SAP Business One\SAPbouiCOM.exe"

using namespace SAPbouiCOM; //I’m not to fond of using namespaces like this but that’s your choice

int _tmain(int argc, _TCHAR* argv[])

{

int hr;

// declare a SboGuiApi

ISboGuiApiPtr oGui;

// decalre a Application

IApplicationPtr oApp;

CoInitialize (NULL); //Init COM Lib Dll's

hr = oGui.CreateInstance ("SAPbouiCOM.SboGuiApi");

if (FAILED(hr))

{

return FALSE; // the create dispatch method failed so return FALSE

}

// connect to the SBO GUI application

oGui->Connect ("0030002C0030002C00530041005000420044005F00440061007400650076002C0050004C006F006D0056004900490056");

// get the recent application

oApp = oGui->GetApplication(0);

//oApp->MessageBox("Conected", 1, "", "", "");

//Declareing form objects

IFormsPtr oForms;

IFormPtr oForm;

IItemsPtr oItems;

IItemPtr oItem;

IButtonPtr oButton;

//IUserDataSourcesPtr lUserDataSources;

//Creating the forms Objects

oForms = oApp->Forms;

//Designing Form

oForm = oForms->Add((_bstr_t)"SimpleForm",ft_Fixed, -1);

oForm->Visible = false;

oItems = oForm->Items;

oForm->Title = "SimpleForm";

oForm->Height = 230;

oForm->Left = 500;

oForm->Top = 250;

oForm->Width = 270;

//Put your items here

//Button

oItem = oItems->Add("Button", it_BUTTON);

oItem->Height = 33;

oItem->Left = 48;

oItem->Top = 152;

oItem->Width = 65;

oButton = oItem->Specific;

oButton->Caption = "Button";

//Text

oItem = oItems->Add("Text", it_EDIT);

oItem->Height = 20;

oItem->Left = 8;

oItem->Top = 8;

oItem->Width = 240;

oItem->RightJustified = -1;

//End of design

oForm->PaneLevel = 1;

oForm->Visible = -1;

return 0;

}

Former Member
0 Kudos

//Here is the DI example

#include <iostream>

#import "C:\Program Files\SAP Manage\SAP Business One DI API\DI API 65\Object\SAPbobsCOM65.dll" rename_namespace ("SAPbobsCOM")

int main()

{

int returnCode;

int hr;

CoInitialize(NULL); //Init COM Lib Dll's

SAPbobsCOM::ICompanyPtr oCompany("SAPbobsCOM.Company");// company pointer

hr = oCompany.CreateInstance ("SAPbobsCOM.Company");

if (FAILED(hr))

{

std::cout << "Failed to create company instance\n";

return FALSE; // the create dispatch method failed so return FALSE

}

// setting logon data to the company object

oCompany->Server = "(local)";

oCompany->CompanyDB = "SBODemo_US";

oCompany->UserName = "manager";

oCompany->Password = "manager";

//oCompany->Language = "ln_English"; //error C2039: 'Language' : is not a member of 'SAPbobsCOM::ICompany'

oCompany->UseTrusted = true;

returnCode = oCompany->Connect();

if (FAILED(returnCode))

std::cout << "Returncode " << returnCode <<'\n';

std::cout << "The connection worked!\n";

return 0;

}

Former Member
0 Kudos

/*

After spending some time with the ui_calculator example I was able to get this example to work. I hope it helps you getting started. Ill try to post a DI example soon to.

*/

#import "C:\Program Files\SAP Manage\SAP Business One\SAPbouiCOM.exe"

using namespace SAPbouiCOM; //Im not to fond of using namespaces like this but thats your choice

int main()

{

int hr;

// declare a SboGuiApi

ISboGuiApiPtr gui;

// decalre a Application

IApplicationPtr con;

CoInitialize (NULL); //Init COM Lib Dll's

hr = gui.CreateInstance ("SAPbouiCOM.SboGuiApi");

if (FAILED(hr))

{

return FALSE; // the create dispatch method failed so return FALSE

}

// connect to the SBO GUI application

gui->Connect ("0030002C0030002C00530041005000420044005F00440061007400650076002C0050004C006F006D0056004900490056");

// get the recent application

con = gui->GetApplication(0);

//Sends a message box to sbo

con->MessageBox("Hello World", 1, "", "", "");

return 0;

}