Establish a development type VC client with SAS interaction

xiaoxiao2021-03-06  22

The last time I wrote how to use VB with SAS, but VB is used to develop a high running efficiency. The system is not ideal. The following will introduce how to use VC to interact with SAS with SAS, only know the VC call COM Method and then refer to my blog's article, don't use me, I know, so "customer" in this article is a rookie programmer.

The first step, of course, introduced this COM type library, otherwise the VC compiler does not know your data type, so so .., import syntax

#import "c: / program files / sas / shared files / integration technology" above is the directory where your SAS type library is located.

In the second step, Microsoft tells me that to call COM must first initialize the COM environment, and finally the COM environment is currently cleared, so you have to write down the following code Coinitialize (null); / * N multi-Code in this intermediate * / ... Couninitialize The third step, of course, is an example of generating a COM object. The method of generating a COM object instance has N, but the following is the most easily, it is best to make first, to get a CLSID CLSID CLSID of COM object; CLSIDFROMPROGID (OLESTR ("SAS COM object, such as SAS.Workspace"); then, use this CLSID, you can generate an object through a smart pointer, as follows, ccomptr _SAS; _SAS.COCREATEINSTANCE (CLSID); OK, object already It is entered, the method of this object can be called, such as _sas-> close (); look, understand, oh, forget, to include header file #include , otherwise it will also report error Let's take a look at this class. This class encapsulates the basic feature of Sas.Workspace.

class CSASPool {public: CComPtr _sas; int CurrRunThreadNum; bool state; CLSID clsid; CSASPool (bool active, CLSID clsid) {this-> clsid = clsid; if (active) {_sas.CoCreateInstance (clsid); (_sas -> async = true; state = true;} else {_sAS = null; state = false;} currrunthreadnum = 0;} void start_sas () {if (! State) {_sas.cocreateInstance (CLSID); _sAS -> LanguageService-> async = true;} state = true;} void exec (char * command) {_SAS-> LanguageService-> Submit (Command);} void close () {_sas-> close ();}};

Then use the following code to call.

#include "stdafx.h"

#include #include "csaspool.h" int Main (int Argc, char * argv []) {Coinitialize (NULL); CLSID CLSID; CLSIDFROMPROGID (OLESTR ("Sas.Workspace", & clsid); CsASPool SAS = CSASPOOL (TRUE, CLSID); SAS.exec ("DATA _NULL_; File 'D: //1.txt'; PUT 'OK'; Run;"); Sas.Close (); couninitialize ();

OVER, this code calls the last result will generate a 1.TXT file in your D. Then it will contain a word OK.

It's finished, but there is a special hidden bug that is included in the program, which is the use of the smart pointer.

As we all know, the smart pointer has encapsulated a perfect pointer, so that we don't have to pay attention to what - reference relationships.

but

Our intelligent pointer _sas life cycle is in

After Couninitialize (), the environment opened by Coinitialize is already in CounInitialize ().

Close, and _SAS, the sector occurs, may result in some problems, so so, the code called above is slightly changed, and it is good.

#include "stdafx.h"

#include #include "csaspool.h" int Main (int Argc, char * argv []) {Coinitialize (NULL); CLSID CLSID; CLSIDFROMPROGID (OLESTR ("Sas.Workspace", & clsid;

{CSASPool SAS = CSASPool (True, ClsID); Sas.exec ("Data _Null_; File 'D: //1.txt'; PUT 'OK'; Run;"); Sas.Close ();} Couninitialize () }

This time, it's true, there is nothing to say.

转载请注明原文地址:https://www.9cbs.com/read-39973.html

New Post(0)